How to put a filehandle into a hash array?

  • Thread starter Vsevolod Afanassiev
  • Start date
V

Vsevolod Afanassiev

I need to build an array of filehandles
so different records go into different output files
depending on a value. This is the code:

$dt = 20030701;

open ( "$FH{$dt}", ">output" . "." . $dt ) or die "$!";

print { "FH{$dt}" } "something ...\n";

close ( "$FH{$dt}" );

It works when run without "-w" but with "-w"
complains of use of uninitialized value.
What's the correct way of doing it?

Thanks
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (Vsevolod Afanassiev) wrote in
I need to build an array of filehandles
so different records go into different output files
depending on a value. This is the code:

$dt = 20030701;

open ( "$FH{$dt}", ">output" . "." . $dt ) or die "$!";

print { "FH{$dt}" } "something ...\n";

close ( "$FH{$dt}" );

It works when run without "-w" but with "-w"
complains of use of uninitialized value.
What's the correct way of doing it?

Why are you putting quotes around $FH{$dt} everywhere?
Lose the quotes and it should work.

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPwP8WWPeouIeTNHoEQIXfQCgja/dLnvGJjnmeuEln0Yh3ekU3OgAoN9N
6RwEEc6oja0OwJnNLwO9B+AY
=6qa/
-----END PGP SIGNATURE-----
 
M

Matthew Browning

I need to build an array of filehandles so different records go into
different output files depending on a value. This is the code:

$dt = 20030701;

open ( "$FH{$dt}", ">output" . "." . $dt ) or die "$!";


If I understand your question correctly, you may find the IO::File
module helpful, which will let you do something like this:

my $file1 = IO::File->new( '>file1' );
my $file2 = IO::File->new( '>file2' );

push @filehandles, ( $file1, $file2 );

for( @filehandles ) {

# write something.
}

....which is a bit more tidy and intuitive.


Matthew Browning.
 
T

Tad McClellan

Vsevolod Afanassiev said:
I need to build an array of filehandles


perldoc -q filehandle

How can I make a filehandle local to a subroutine?
How do I pass filehandles between subroutines?
How do I make an array of filehandles?

open ( "$FH{$dt}", ">output" . "." . $dt ) or die "$!";
^ ^
^ ^

Why are you forcing stringification of the hash value?

See also:

perldoc -q quoting

What's wrong with always quoting "$vars"?

print { "FH{$dt}" } "something ...\n";


You know that you are not accessing that same (or any) hash
value here, right?

It works when run without "-w"


Then it will work _with_ warnings too, since warnings never change
how your program executes...

What's the correct way of doing it?


The way the FAQ answer shows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top