Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^4: Naming file handles with variables?

by QM (Parson)
on May 01, 2009 at 14:09 UTC ( #761297=note: print w/replies, xml ) Need Help??

in reply to Re^3: Naming file handles with variables?
in thread Naming file handles with variables?

OIC. Scalar context giving the next index. Hadn't seen this before, but it's interesting.

I don't think I would use this myself, as I don't know who would be maintaining my code. I'd prefer the alternative you gave for transparency. Doubly preferred, given the OP's apparent sophistication level.

Quantum Mechanics: The dreams stuff is made of

  • Comment on Re^4: Naming file handles with variables?

Replies are listed 'Best First'.
Re^5: Naming file handles with variables?
by GrandFather (Sage) on May 01, 2009 at 22:15 UTC

    I agree with you. I probably should have used the less esoteric version given the OP's demonstrated level of expertise. However, you now know a new technique, so maybe pushing the edge of the envelope a little was the right thing after all. ;)

    True laziness is hard work

      Interesting technique. Are there any advantages to $foo[@foo] = 'var' over push @foo, 'var'


      Anyone foolish enough to change the starting index of their arrays with "$[" will find that $foo[@foo] = 'var' breaks. But then they deserve what they get. However, push and map will function even in the dire weirdness imposed by defining arrays to start at index 23.

      For completeness sake, here's the map implementation, even though you know perfectly well how to do it.

      my @fileHandles = map { open my $fh, '<', $_ or die "Can't open $filename $!"; $fh; } @fileNames;

      Update: fixed dumb typos, thanks to GrandFather.

      TGI says moo

        Are there any advantages to $foo@foo = 'var' over push @foo, 'var'?

        Maybe if you are playing golf? In the context I used it using push meant using a temporary variable and another line for the push so in that context there may be an advantage if you are comfortable with the technique.

        I suspect the map technique may be more prone to error than the $foo[@foo] technique. ;)

        True laziness is hard work
        I like the map idea, though I'd probably end up with a subroutine. Here's what I find myself doing:
        sub open_for_write { my @filenames = @_; my @fh; # or my %fh; for my $fn (@filenames) { my $fh; # does it exist? # is it zipped? # open for append? # open with create? $fh[@fh] = $fh; # wink, wink # or $fh{$fn} = $fh; } return @fh; # or return \%fh; ############ my @filehandles = open_for_write( @filenames ); # or my $filehandles_hashref = open_for_write( @filenames );

        Quantum Mechanics: The dreams stuff is made of

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://761297]
[erix]: that's bad... I can only wish you good luck
[Discipulus]: not even in Canada is easy to find a decent job
Discipulus was a question
[GotToBTru]: current job found me
Discipulus is so lucky to have a decent job, with old, favorable contract, done before the ultralibersm arrived in Eataly
[Lady_Aleena]: Has anyone here ever use pcregrep? I can't seem to get the --include regex right. --include=*.p[lm] works in grep but not pcregrep.
[erix]: to lift your spirits, here is some more happy Randy news...
[Lady_Aleena]: The reason I would like to use pcregrep is because it can do multiline searches supposedly.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2017-05-23 19:15 GMT
Find Nodes?
    Voting Booth?