The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

File::Macro - Read a file within a block scope

VERSION

Version 0.01

SYNOPSIS

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 $_.

    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>;
    };

EXPORT

Exports only with_file.

SUBROUTINES/METHODS

with_file $file_name => [ $file_mode => ] sub { }

 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>;
  } );

AUTHOR

Jeff Goff, <jgoff at cpan.org>

BUGS

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.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc File::Macro

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

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.