I personally believe times could be ready for an argv pragma. Every now and again it occurs to me. The other day, in a reply I suggested a sub as follows:
sub domkfile { my $fname=shift; open my $fh, '<', $fname or die "Can't open `$fname': $!\n"; join ' ' => map /SOURCE=\.\\(.*)$/i, <$fh>; }
I was about to suggest that to make it more general, it may accept, rather than a single filename, a list of filenames, and take advantage of the magic *ARGV variables:
sub domkfile { local @ARGV = @_; join ' ' => map /SOURCE=\.\\(.*)$/i, <>; }
Look! Even simpler! But then people would have complained: they always do. They say that a explicit opens are clearer. I beg to disagree: @ARGV and <> are deeply rooted mechanisms in the brain of every Perl programmer. Of course one may write a separate sub, or module -and perhaps some exist already (well, File::Slurp could be used here)- to slurp in lines from several files. But the above code is still more compact and yet clearly readable.
One argument I can buy, instead, is that the implicit open's associated with the ARGV filehandle are two args ones: while the implications of this are clear when it is used in a "top level" script (perhaps a very short one taking advantage of the -n or -p switches) and are often even desirable, its use in a sub, as in this case, can make them much more controversial.
Now, I think that this could be amended by an argv pragma which would locally change the nature of *ARGV's implicit open's, setting it explicitly to one type or the other, and perhaps do other thing, like setting layers. The above sub, e.g. could simply become:
sub domkfile { use argv '3args'; local @ARGV = @_; join ' ' => map /SOURCE=\.\\(.*)$/i, <>; }
A crazy idea?
In reply to How 'bout an argv pragma? by blazar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |