perlquestion
why_bird
Hi Monks
<p>I'm struggling a little with typeglobbing and filehandles. I've read [id//8874|this] excellent node and more besides</p>
<p> What I can't figure out is why does this NOT work:
<code>
#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);
</code>
(And it also doesn't work if you try any of the following:
<ul>
<li> <c>*FILE_A -> \*FILE_A</c></li>
<li> <c>$FH[$i] -> *$FH[$i]</c></li>
<li> <c>$FH[$i] -> ${FH[$i]}</c></li>
<li> <c>*FILE_A -> \*FILE_A && $FH[$i] -> *$FH[$i]</c></li>
</ul>
etc.. the point being I've tried quite a few things.
Specifically the problem is that <tt>File::glob::csh_glob</tt> returns <tt>undef</tt>, <b>except</b> when you do
<ul>
<li><c>*FILE_A -> \*FILE_A</c></li>
</ul>
when it returns "GLOB(0x52f9f0)" (obv memory address changes..), but then the output is <tt>line: GLOB(0x52f9f0)</tt></p>
<p>
However, this <i>works</i>!!
<code>
#...as before...
for(my $i=0;$i<2;$i++){
my $fh=$FH[$i]; # <----**
while(my $line=<$fh>){
#...as before...
</code>
And it still works whether <ul><li><c>*FILE_A --> \*FILE_A</c></li></ul> or not...</p>
<p>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??</p>
Cheers <br>
why_bird
<p><b>edit:</b> Formatting issues!</p>
<p><b>edit:</b> ">" to "<", thanks [Fletch]</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-670682">
<small><center> ........<br>
Those are my principles. If you don't like them I have others.<br>
-- <b>Groucho Marx</b><br>
.......<br></center></small>
</div></div>