/usr/share/perl5/IO/Detect.pm is in libscalar-does-perl 0.200-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | package IO::Detect;
use 5.008;
use constant { false => !1, true => !0 };
use strict;
use warnings;
use if $] < 5.010, 'UNIVERSAL::DOES';
BEGIN {
$IO::Detect::AUTHORITY = 'cpan:TOBYINK';
$IO::Detect::VERSION = '0.200';
}
use namespace::clean 0.19;
EXPORTER:
{
use base "Exporter::TypeTiny";
our %_CONSTANTS;
our @EXPORT = qw( is_filehandle is_filename is_fileuri );
our @EXPORT_OK = (
qw( is_filehandle is_filename is_fileuri ),
qw( FileHandle FileName FileUri ),
qw( ducktype as_filehandle ),
);
our %EXPORT_TAGS = (
smartmatch => [qw( FileHandle FileName FileUri )],
);
sub _exporter_expand_sub
{
my $class = shift;
return ducktype => $class->_build_ducktype(@_[0,1]) if $_[0] eq "ducktype";
return as_filehandle => $class->_build_as_filehandle(@_[0,1]) if $_[0] eq "as_filehandle";
$class->SUPER::_exporter_expand_sub(@_);
}
sub _exporter_validate_opts
{
require B;
my $class = shift;
$_[0]{exporter} ||= sub {
my $into = $_[0]{into};
my ($name, $sym) = @{ $_[1] };
for (grep ref, $into->can($name))
{
B::svref_2object($_)->STASH->NAME eq $into
and _croak("Refusing to overwrite local sub '$name' with export from $class");
}
"namespace::clean"->import(-cleanee => $_[0]{into}, $name);
no strict qw(refs);
no warnings qw(redefine prototype);
*{"$into\::$name"} = $sym;
}
}
}
use overload qw<>;
use Scalar::Util qw< blessed openhandle reftype >;
use Carp qw<croak>;
use URI::file;
sub _lu {
require lexical::underscore;
goto \&lexical::underscore;
}
sub _ducktype
{
my ($object, $methods) = @_;
return unless blessed $object;
foreach my $m (@{ $methods || [] })
{
return unless $object->can($m);
}
return true;
}
sub _build_ducktype
{
my ($class, $name, $arg) = @_;
my $methods = $arg->{methods};
return sub (;$) {
@_ = ${+_lu} unless @_;
push @_, $methods;
goto \&_ducktype;
};
}
my $expected_methods = [
qw(close eof fcntl fileno getc getline getlines ioctl read print stat)
];
sub is_filehandle (;$)
{
my $fh = @_ ? shift : ${+_lu};
return true if openhandle $fh;
# Logic from IO::Handle::Util
{
my $reftype = reftype($fh);
$reftype = '' unless defined $reftype;
if ($reftype eq 'IO'
or $reftype eq 'GLOB' && *{$fh}{IO})
{
for ($fh->fileno, fileno($fh))
{
return unless defined;
return unless $_ >= 0;
}
return true;
}
}
return true if blessed $fh && $fh->DOES('IO::Handle');
return true if blessed $fh && $fh->DOES('FileHandle');
return true if blessed $fh && $fh->DOES('IO::All');
return _ducktype $fh, $expected_methods;
}
sub _oneline ($)
{
!! ( $_[0] !~ /\r?\n|\r/s )
}
sub is_filename (;$)
{
my $f = @_ ? shift : ${+_lu};
return true if blessed $f && $f->DOES('IO::All');
return true if blessed $f && $f->DOES('Path::Class::Entity');
return ( length "$f" and _oneline "$f" )
if blessed $f && overload::Method($f, q[""]);
return ( length $f and _oneline $f )
if defined $f && !ref $f;
return;
}
sub is_fileuri (;$)
{
my $f = @_ ? shift : ${+_lu};
return $f if blessed $f && $f->DOES('URI::file');
return URI::file->new($f->uri) if blessed $f && $f->DOES('RDF::Trine::Node::Resource');
return URI::file->new($f) if $f =~ m{^file://\S+}i;
return;
}
sub _build_as_filehandle
{
my ($class, $name, $arg) = @_;
my $default_mode = $arg->{mode} || '<';
return sub (;$$)
{
my $f = @_ ? shift : ${+_lu};
return $f if is_filehandle($f);
if (my $uri = is_fileuri($f))
{ $f = $uri->file }
my $mode = shift || $default_mode;
open my $fh, $mode, $f
or croak "Cannot open '$f' with mode '$mode': $!, died";
return $fh;
};
}
*as_filehandle = __PACKAGE__->_build_as_filehandle('as_filehandle', +{});
{
package IO::Detect::SmartMatcher;
BEGIN {
$IO::Detect::SmartMatcher::AUTHORITY = 'cpan:TOBYINK';
$IO::Detect::SmartMatcher::VERSION = '0.200';
}
use Scalar::Util qw< blessed >;
use overload (); no warnings 'overload'; # '~~' unavailable in Perl 5.8
use overload
'""' => 'to_string',
'~~' => 'check',
'==' => 'check',
'eq' => 'check',
fallback => 1;
sub check
{
my ($self, $thing) = @_;
$self->[1]->($thing);
}
sub to_string
{
shift->[0]
}
sub new
{
my $proto = shift;
if (blessed $proto and $proto->isa(__PACKAGE__))
{
return "$proto"->new(@_);
}
bless \@_ => $proto;
}
}
use constant FileHandle => IO::Detect::SmartMatcher::->new(FileHandle => \&is_filehandle);
use constant FileName => IO::Detect::SmartMatcher::->new(FileName => \&is_filename);
use constant FileUri => IO::Detect::SmartMatcher::->new(FileUri => \&is_fileuri);
true;
__END__
=pod
=encoding utf8
=for stopwords frickin' filehandliness
=head1 NAME
IO::Detect - is this a frickin' filehandle or what?!
=head1 SYNOPSIS
use IO::Detect;
if (is_filehandle $fh)
{
my $line = <$fh>;
}
=head1 DESCRIPTION
It is stupidly complicated to detect whether a given scalar is
a filehandle (or something filehandle like) in Perl. This module
attempts to do so, but probably falls short in some cases. The
primary advantage of using this module is that it gives you
somebody to blame (me) if your code can't detect a filehandle.
The main use case for IO::Detect is for when you are writing
functions and you want to allow the caller to pass a file as
an argument without being fussy as to whether they pass a file
name or a file handle.
=head2 Functions
Each function takes a single argument, or if called with no
argument, operates on C<< $_ >>.
=over
=item C<< is_filehandle $thing >>
Theoretically returns true if and only if $thing is a file handle,
or may be treated as a filehandle. That includes blessed references
to filehandles, things that inherit from IO::Handle, etc.
It's never going to work 100%. What Perl allows you to use as a
filehandle is mysterious and somewhat context-dependent, as the
following code illustrates.
my $fh = "STD" . "OUT";
print $fh "Hello World!\n";
=item C<< is_filename $thing >>
Returns true if $thing is a L<IO::All> object or L<Path::Class::Entity>
or L<any non-reference, non-zero-length string with no line breaks>.
That's because depending on your operating system, virtually anything
can be used as a filename. (In fact, on many systems, including Linux,
filenames can contain line breaks. However, this is unlikely to be
intentional.)
This function doesn't tell you whether $thing is an existing file on
your system. It attempts to tell you whether $thing could possibly be
a filename on some system somewhere.
=item C<< is_fileuri $thing >>
Returns true if $thing is a URI beginning with "file://". It
allows for L<URI> objects, L<RDF::Trine::Node::Resource> objects,
strings and objects that overload stringification.
This function actually returns an "interesting value of true". The
value returned is a L<URI::file> object.
=item C<< as_filehandle $thing, $mode >>
Returns $thing if it is a filehandle; otherwise opens it with mode
$mode (croaking if it cannot be opened). $mode defaults to "<" (read
access).
This function is not exported by default, but needs to be requested
explicitly:
use IO::Detect qw(as_filehandle);
You may even specify a different default mode, or import it several
times with different names:
use IO::Detect
as_filehandle => { -as => 'as_filehandle_read', mode => '<' },
as_filehandle => { -as => 'as_filehandle_write', mode => '>' };
=back
=head2 Smart Matching
You can import three constants for use in smart matching:
use IO::Detect -smartmatch;
These constants are:
=over
=item C<< FileHandle >>
=item C<< FileName >>
=item C<< FileUri >>
=back
They can be used like this:
if ($file ~~ FileHandle)
{
...
}
Note that there does exist a L<FileHandle> package in Perl core. This
module attempts to do the right thing so that C<< FileHandle->new >>
still works, but there are conceivably places this could go wrong, or
be plain old confusing.
Although C<is_filehandle> and its friends support Perl 5.8 and above,
smart match is only available in Perl 5.10 onwards.
=head2 Use with Scalar::Does
The smart match constants can also be used with L<Scalar::Does>:
if (does $file, FileHandle)
{
...;
}
elsif (does $file, FileName)
{
...;
}
=head2 Precedence
Because there is some overlap/ambiguity between what is a filehandle
and what is a filename, etc, if you need to detect between them, I
recommend checking C<is_filehandle> first, then C<is_fileuri> and
falling back to C<is_filename>.
for ($file)
{
when (FileHandle) { ... }
when (FileUri) { ... }
when (FileName) { ... }
default { die "$file is not a file!" }
}
=head2 Export
Like Scalar::Does, IO::Detect plays some tricks with L<namespace::clean> to
ensure that any functions it exports to your namespace are cleaned up when
you're finished with them.
=head3 Duck Typing
In some cases you might be happy to accept something less than a
complete file handle. In this case you can import a customised
"duck type" test...
use IO::Detect
-default,
ducktype => {
-as => 'is_slurpable',
methods => [qw(getlines close)],
};
sub do_something_with_a_file
{
my $f = shift;
if ( is_filehandle $f or is_slurpable $f )
{ ... }
elsif ( is_filename $f )
{ ... }
}
Duck type test functions only test that the argument is blessed
and can do all of the specified methods. They don't test any other
aspect of "filehandliness".
=head1 BUGS
Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=IO-Detect>.
=head1 SEE ALSO
This module is an attempt to capture some of the wisdom from this
PerlMonks thread L<http://www.perlmonks.org/?node_id=980665> into
executable code.
Various other modules that may be of interest, in no particular
order...
L<Scalar::Does>,
L<Scalar::Util>,
L<FileHandle>,
L<IO::Handle>,
L<IO::Handle::Util>,
L<IO::All>,
L<Path::Class>,
L<URI::file>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2012-2013 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|