Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: globbing filehandles in arrays

by ikegami (Patriarch)
on Jan 23, 2009 at 13:50 UTC ( [id://738484]=note: print w/replies, xml ) Need Help??


in reply to globbing filehandles in arrays

<> is two different operators. Based on its operand, it's an alias for readline or it's an alias for glob. Here are two ways of forcing readline:

while (my $line = readline($FH[$i])) { ... }
my $fh = $FH[$i]; while (my $line = <$fh>) { ... }

There's still the question of why you are using global variables for no reason.

open(FILE_A,">","a.txt") || ...; open(FILE_B,">","b.txt") || ...; my @FH=(*FILE_A,*FILE_B);

should be

open(my $FILE_A,">","a.txt") || ...; open(my $FILE_B,">","b.txt") || ...; my @FH=($FILE_A,$FILE_B);

You're using 3-arg open which was introduced at the same time as lexical file handles, so it's not a backwards compatibility issue.

Replies are listed 'Best First'.
Re^2: globbing filehandles in arrays
by jwkrahn (Abbot) on Jan 23, 2009 at 14:04 UTC

    Or perhaps:

    my @FH; for my $file ( qw/a.txt b.txt/ ) { open( $FH[ @FH ], '>', $file ) || ...; }
Re^2: globbing filehandles in arrays
by why_bird (Pilgrim) on Jan 26, 2009 at 08:35 UTC
    You're using 3-arg open which was introduced at the same time as lexical file handles, so it's not a backwards compatibility issue.

    Wow, I have to say I'm surprised that I really didn't know about something as basic as that!! Just goes to show what perlmonks can teach you. Strange also because I've found the global non-lexical file handles a real pain in the past and have never realised you could do it like that. I consider myself well and truly corrected (and pleased!)

    For future reference, here is the (most?) relevent perldoc documentation, which strangely I hadn't come across (or perhaps rather read thoroughly) before. Also, perlopentut says the above explicitly, which I notice now that I come to look for it!

    It still seems to be true though that the vast majority of examples in open and perlopentut are given using those old global filehandles, which I find a bit strange if it's not 'recommended' behaviour (I would certainly not recommend it!). Is this just because the documentation is old, or is there another reason?

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

      Probably just because noone got around to doing it ...until recently. It's finally been fixed in bleed (5.12 to be) and backported to maint-5.10 (5.10.1 to be).

      There's still lots of example with non-lexical file handles if you're looking for something to do :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://738484]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found