Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^6: Naming file handles with variables?

by TGI (Parson)
on May 03, 2009 at 15:28 UTC ( #761595=note: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re^7: Naming file handles with variables?
by GrandFather (Sage) on May 03, 2009 at 21:19 UTC
    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
Re^7: Naming file handles with variables?
by QM (Parson) on May 04, 2009 at 20:44 UTC
    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://761595]
Discipulus is a lazy sysadmin but not lazy as marto coworkers..
[marto]: the sysadmins are actually a different 3rd party company
[Discipulus]: oh yes, outsurcing.. i remember: not as lazy as marto outsourced ones..

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2017-01-24 11:14 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (204 votes). Check out past polls.