I kicked all of these around some, and I'm still confused. Possibly this is a local issue to me, possibly not. In summary, I got ar0n's technique to work when no file was being read from the command line (in the debugger, at least), but not the modification suggested by merlyn and tye (who I would not dream of ignoring), and neither when a file was being read. But tye says it works for him, and tye is an honorable man...
So, below are what I did and what happened, and if I'm making a stupid mistake I'll be very happy to find out what it is. I'm only including the results trying to read from two files at once, because it's long enough already, and the other ones don't add anything much.
For reference,
This is perl, version 5.004_04 built for IP27-irix
Pseudo-update: and it appears that it is indeed relevant. Oh, why can't they install 5.6 here?Oh well, at least I'm not confused any more. :-)
| Suggestion by |
merlyn and tye
do { local *ARGV; @ARGV =$name;<> }
|
ar0n my @array = do { local @ARGV = $name; <> };
|
| My implementation |
#!/usr/bin/perl -w
use strict;
my $name = "file_b";
my ($foo1, $foo2, $foo3);
$foo1 = <>;
$foo2= <>;
my @lines= do { local *ARGV; @ARGV =$name;<> };
$foo3 = <>;
print "Lines:\n",@lines;
print "\nFoos:\n",$foo1,$foo2,$foo3;
|
#!/usr/bin/perl -w
use strict;
my $name = "file_b";
my ($foo1, $foo2, $foo3);
$foo1 = <>;
$foo2= <>;
my @lines= do { local @ARGV =$name;<> };
$foo3 = <>;
print "Lines:\n",@lines;
print "\nFoos:\n",$foo1,$foo2,$foo3;
|
| My results |
% perl test.pl file_a
Lines:
Foos:
File A line 1
File A line 2
File A line 3
|
% perl test.pl file_a
Lines:
File A line 3
File A line 4
File A line 5
File A line 6
File A line 7
File A line 8
File A line 9
File A line 10
File B line 1
File B line 2
File B line 3
File B line 4
File B line 5
File B line 6
File B line 7
File B line 8
File B line 9
File B line 10
Foos:
File A line 1
File A line 2
Use of uninitialized value at test.pl line 16.
|
If God had meant us to fly, he would *never* have give us the railroads.
--Michael Flanders
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.