Adding a standard Perl file open function to each script I have is a bit annoying:
sub openfile{
(my $filename) = @_;
open FILE,\"$filename\" or die
For quick and dirty, I rather like the simplicity of mucking with @ARGV
.
# Ysth is right, it doesn't automatically die; I need another line.
use 5.010;
use strict;
my @rows = do {
use warnings FATAL => 'inplace'; # oddly enough, this is the one. ??
@ARGV='/a/file/somewhere';
<>;
};
say q(Not gettin' here.);
If perl* cannot open the file, it automatically dies.
* - the executable, so please don't capitalize.
You might also want to consider using Tie::File, particularly if you are reading larger files and don't want to read the entire file into memory. It's a core module. Also, please refer to perlfaq5.