Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi Monks

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:

#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);
(And it also doesn't work if you try any of the following:
  • *FILE_A    -> \*FILE_A
  • $FH[$i]    -> *$FH[$i]
  • $FH[$i]    -> ${FH[$i]}
  • *FILE_A    -> \*FILE_A && $FH[$i] -> *$FH[$i]
etc.. the point being I've tried quite a few things. Specifically the problem is that File::glob::csh_glob returns undef, except when you do
  • *FILE_A -> \*FILE_A
when it returns "GLOB(0x52f9f0)" (obv memory address changes..), but then the output is line: GLOB(0x52f9f0)

However, this works!!

#...as before... for(my $i=0;$i<2;$i++){ my $fh=$FH[$i]; # <----** while(my $line=<$fh>){ #...as before...
And it still works whether
  • *FILE_A --> \*FILE_A
or not...

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??

Cheers
why_bird

edit: Formatting issues!

edit: ">" to "<", thanks Fletch

........
Those are my principles. If you don't like them I have others.
-- Groucho Marx
.......

In reply to globbing filehandles in arrays by why_bird

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-28 18:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found