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?

--
If you can't understand the incipit, then please check the IPB Campaign.

In reply to How 'bout an argv pragma? by blazar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.