|
NAMEIO::Detect - is this a frickin' filehandle or what?!SYNOPSISuse IO::Detect; if (is_filehandle $fh) { my $line = <$fh>; } DESCRIPTIONIt 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. FunctionsEach function takes a single argument, or if called with no argument, operates on $_.
Smart MatchingYou can import three constants for use in smart matching:use IO::Detect -smartmatch; These constants are:
They can be used like this: if ($file ~~ FileHandle) { ... } Note that there does exist a FileHandle package in Perl core. This module attempts to do the right thing so that "FileHandle->new" still works, but there are conceivably places this could go wrong, or be plain old confusing. Although "is_filehandle" and its friends support Perl 5.8 and above, smart match is only available in Perl 5.10 onwards. Use with Scalar::DoesThe smart match constants can also be used with Scalar::Does:if (does $file, FileHandle) { ...; } elsif (does $file, FileName) { ...; } PrecedenceBecause 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 "is_filehandle" first, then "is_fileuri" and falling back to "is_filename".for ($file) { when (FileHandle) { ... } when (FileUri) { ... } when (FileName) { ... } default { die "$file is not a file!" } } ExportLike Scalar::Does, IO::Detect plays some tricks with namespace::clean to ensure that any functions it exports to your namespace are cleaned up when you're finished with them.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". BUGSPlease report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=IO-Detect>.SEE ALSOThis module is an attempt to capture some of the wisdom from this PerlMonks thread <http://www.perlmonks.org/?node_id=980665> into executable code.Various other modules that may be of interest, in no particular order... Scalar::Does, Scalar::Util, FileHandle, IO::Handle, IO::Handle::Util, IO::All, Path::Class, URI::file. AUTHORToby Inkster <tobyink@cpan.org>.COPYRIGHT AND LICENCEThis software is copyright (c) 2012-2014, 2017 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. DISCLAIMER OF WARRANTIESTHIS 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.
Visit the GSP FreeBSD Man Page Interface. |