why_bird has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks
why_bird
........
Those are my principles. If you don't like them I have others.
-- Groucho Marx
.......
I'm struggling a little with typeglobbing and filehandles. I've read this excellent node and more besides
What I can't figure out is why does this NOT work:
(And it also doesn't work if you try any of the following:#assume files a.txt and b.txt exist and are normal, readable files. use strict; use warnings; open(FILE_A,"<","a.txt") || die "Could not open file 'a.txt': $!\n"; open(FILE_B,"<","b.txt") || die "Could not open file 'b.txt': $!\n"; my @FH=(*FILE_A,*FILE_B); for(my $i=0;$i<2;$i++){ while(my $line=<$FH[$i]>){ print "line: $line\n"; } } exit(0);
- *FILE_A -> \*FILE_A
- $FH[$i] -> *$FH[$i]
- $FH[$i] -> ${FH[$i]}
- *FILE_A -> \*FILE_A && $FH[$i] -> *$FH[$i]
- *FILE_A -> \*FILE_A
However, this works!!
And it still works whether#...as before... for(my $i=0;$i<2;$i++){ my $fh=$FH[$i]; # <----** while(my $line=<$fh>){ #...as before...
- *FILE_A --> \*FILE_A
Why? What's the difference? Am I missing something fundamental here? Or is this some kind of 'feature'? Or is the first syntax ambiguous, or.. what??
Cheerswhy_bird
edit: Formatting issues!
edit: ">" to "<", thanks Fletch
Those are my principles. If you don't like them I have others.
-- Groucho Marx
.......
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: globbing filehandles in arrays
by almut (Canon) on Jan 23, 2009 at 13:44 UTC | |
by why_bird (Pilgrim) on Jan 26, 2009 at 08:38 UTC | |
Re: globbing filehandles in arrays
by ikegami (Patriarch) on Jan 23, 2009 at 13:50 UTC | |
by jwkrahn (Abbot) on Jan 23, 2009 at 14:04 UTC | |
by why_bird (Pilgrim) on Jan 26, 2009 at 08:35 UTC | |
by ikegami (Patriarch) on Jan 26, 2009 at 13:54 UTC | |
Re: globbing filehandles in arrays
by Fletch (Bishop) on Jan 23, 2009 at 13:42 UTC | |
by why_bird (Pilgrim) on Jan 26, 2009 at 08:14 UTC | |
by Fletch (Bishop) on Jan 27, 2009 at 03:23 UTC |
Back to
Seekers of Perl Wisdom