Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Can a filehandle name be dynamic?

by piyush (Novice)
on Sep 24, 2009 at 11:52 UTC ( #797174=perlquestion: print w/ replies, xml ) Need Help??
piyush has asked for the wisdom of the Perl Monks concerning the following question:

Hi ...m facing a scenario wherein I want to open a file with a filehandle name that is dynamic:
- open(FILEHANDLE$i, ">>$filename");
where $i is a counter variable.

Is such a thing possible? ..
Thanks in advance....

Comment on Can a filehandle name be dynamic?
Re: Can a filehandle name be dynamic?
by apl (Monsignor) on Sep 24, 2009 at 12:07 UTC
    Why don't you try writing a test script?
Re: Can a filehandle name be dynamic?
by ELISHEVA (Prior) on Sep 24, 2009 at 12:11 UTC

    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

      It should work with print {$aFiles[$_]} "Hello World\n"; without the need for an extra variable.
      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 ;)

Re: Can a filehandle name be dynamic?
by rovf (Priest) on Sep 24, 2009 at 12:36 UTC

    Wouldn't IO::File effectively do what you want? I don't think you worry about the actual name of the filehandle, but don't want the number of handles needed at compiletime - right?

    -- 
    Ronald Fischer <ynnor@mm.st>
      The number of filehandles get decided at runtime...
Re: Can a filehandle name be dynamic?
by Anonymous Monk on Sep 24, 2009 at 14:36 UTC
Re: Can a filehandle name be dynamic?
by GrandFather (Cardinal) on Sep 24, 2009 at 21:04 UTC

    ELISHEVA has already shown you the way, but it's worth commenting on some of the important style elements in Beth's code.

    First off, note that she uses strictures (use strict; use warnings;) - you should always use strictures!

    Note too that Beth uses the three parameter version of open and that she checks the result. Always use the three parameter version of open. Not so obvious because of the nature of the question is that a lexical variable is being used for the file handle - always use lexical variables and declare them in the smallest scope possible.


    True laziness is hard work

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-07-29 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls