So you think localizing @ARGV should also localize the (potentially open) ARGV filehandle too?

I personally believe that based on the principle of least surprise, I would indeed say so. Perl does magic worse than that already, and otherwise a localised @ARGV would be of little utility since one would want to use the above described technique (referring to your sub) as a cheap shortcut to

sub collect { join '', map { open my $fh, '<', $_ or warn "Can't open `$_': $!\n"; <$fh>; } @_; }

(But for the 2-args implicit openedness, of course.)

What exactly did you try? Localizing *ARGV worked for me exactly as I would have thought:

It doesn't work for me:

picard:~/tmp [11:26:38]$ perl -v | head -n 2 This is perl, v5.10.0 built for i486-linux-gnu-thread-multi picard:~/tmp [11:26:42]$ cat ../domk.pl #!/usr/bin/perl use strict; use warnings; while (<>) { chomp; print "huzzah:".collect( $_ )."!\n"; } sub collect { local *ARGV; @ARGV = @_; join '', <>; } __END__ picard:~/tmp [11:26:55]$ ls file? | ../domk.pl Can't open file1: No such file or directory at ../domk.pl line 14. huzzah:! Can't open file2: No such file or directory at ../domk.pl line 14. huzzah:! Can't open : No such file or directory at ../domk.pl line 14. huzzah:!

But even if it did "work", as it did with you and kyle, it wouldn't be much dwimmy: in fact your output is not the same one would have got out of using the "expanded" sub above. The following example won't work on my box either, but you could try it:

#!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; sub dargv { local *ARGV; @ARGV = @_; <>; } sub dopen { map { open my $fh, '<', $_ or warn "Can't open `$_': $!\n"; <$fh>; } @_; } while (<>) { chomp; is_deeply [dargv $_], [dopen $_] => $_; } __END__

Update: dopen has issues; this was discussed in another thread.

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

In reply to Re^4: How 'bout an argv pragma? by blazar
in thread 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.