Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Can a filehandle name be dynamic?

by ELISHEVA (Prior)
on Sep 24, 2009 at 12:11 UTC ( #797184=note: print w/ replies, xml ) Need Help??


in reply to Can a filehandle name be dynamic?

You can store a filehandle is a variable, so why not use an array of filehandles, e.g. open($aFiles[$i], ">>"$filename")?

Note: apparently print doesn't like print $aFiles[$i] "Some text" so you will have to assign the file handle to a scalar before using it, something like this:

use strict; use warnings; my @aFiles; for (0..2) { my $sFile = "foo.$_"; open($aFiles[$_], '>', $sFile) or die "$sFile: $!"; my $fh = $aFiles[$_]; #make print happy print $fh "Hello World\n"; close $fh; }

Best, beth


Comment on Re: Can a filehandle name be dynamic?
Select or Download Code
Re^2: Can a filehandle name be dynamic?
by goeb (Pilgrim) on Sep 24, 2009 at 12:35 UTC
    It should work with print {$aFiles[$_]} "Hello World\n"; without the need for an extra variable.
Re^2: Can a filehandle name be dynamic?
by almut (Canon) on Sep 24, 2009 at 13:47 UTC
    apparently print doesn't like print $aFiles[$i] "Some text"

    Similar syntactic issues arise with

    my $line = <$fh[$i]>;

    The workaround here - if you don't want to use an extra intermediate variable - would be to use

    my $line = readline $fh[$i];

    <{$fh[$i]}> wouldn't work here.


    Side note: I often see people from the Anti-Perl League get overly upset about those minor inconsistencies in the language...  But methinks, could you love someone who has no quirks whatsoever? Well, I couldn't. Ditto with programming languages ;)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-08-29 16:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (282 votes), past polls