File::Macro - Read a file within a block scope
Version 0.01
This module exists exclusively to provide a shorthand for the open... or die idiom. Instead of repeating the same boilerplate, you simply call with_file and do whatever you need to do with your file inside the block, which is already opened for you in $_.
open... or die
with_file
$_
use File::Macro; with_file 'foo.csv' => '<' => sub { say <$_>; };
If you want to use a different variable for the filehandle, just specify it after the mode selector, like so:
my $fh; with_file 'foo.csv' => '<' => \$fh => sub { say <$fh>; };
Exports only with_file.
Opens file C<$file_name> in mode C<$file_mode> for reading, assigns the filehandle to C<$_>. Once the function in the second argument is complete, perl closes the filehandle automatically. The C<$file_mode> defaults to C<< < >>, that is to say, reading. You can also pass in a reference to your own file handle as follows: with_file( 't/01-base.t', '<', \$my_fh, sub { say <$my_fh>; } );
Jeff Goff, <jgoff at cpan.org>
<jgoff at cpan.org>
Please report any bugs or feature requests to bug-file-macro at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Macro. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-file-macro at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc File::Macro
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Macro
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/File-Macro
CPAN Ratings
http://cpanratings.perl.org/d/File-Macro
Search CPAN
http://search.cpan.org/dist/File-Macro/
Copyright 2014 Jeff Goff.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install File::Macro, copy and paste the appropriate command in to your terminal.
cpanm
cpanm File::Macro
CPAN shell
perl -MCPAN -e shell install File::Macro
For more information on module installation, please visit the detailed CPAN module installation guide.