Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storeing filehandles in a hash 1

Status
Not open for further replies.

Tve

Programmer
May 22, 2000
166
FR
Hi,

I need to create a bunch of temporary files to store information. Best solution seems to be File::Temp, as the files will automatically be removed when script ends/dies.

The thing is, I would like to store the filehandle references in a hash, this would make it easy to maintain, loop, ...

I have to admit, I'm not well versed in FileHandles and/or references to FileHandles. This is what I would like to do:
Code:
#!/usr/bin/perl -w

# Modules
  use strict;
  use File::Temp qw/tempfile/;

# Declare variables
  my (%tempfiles);

# Loop and create handles
  foreach ('host','user','feature') {

  # Make a temporary file
    my ($fh, $filename) = tempfile( DIR => $ENV{TEMP} );

  # Store in global variable
    $tempfiles{$_}{'handle'}   = $fh;
    $tempfiles{$_}{'filename'} = $filename;

   }

# Printout to file
  print $tempfiles{host}{handle} "Hello World!\n";

But I get the following error:

String found where operator expected at test.pl line 23, near "} "Hello World!\n
""
(Missing operator before "Hello World!\n"?)
syntax error at test.pl line 23, near "} "Hello World!\n""
Execution of test.pl aborted due to compilation errors.


I must have forgotten a \,$ or * somewhere.

Can anybody help?

AD AUGUSTA PER ANGUSTA

Thierry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top