in reply to Re^2: How can you check to see if a file handle is already open?
in thread How can you check to see if a file handle is already open?

agent_smith,
You may find Performance Trap - Opening/Closing Files Inside a Loop useful.

Cheers - L~R

  • Comment on Re^3: How can you check to see if a file handle is already open?

Replies are listed 'Best First'.
Re^4: How can you check to see if a file handle is already open?
by agent_smith (Scribe) on Jan 16, 2008 at 22:16 UTC
    Limbic~Region++,

    That thread was perfect! Thank you for pointing it out.

    while(<INPUT>) { chomp $_; if ( $_ =~ m#^(\d+)/(\d+)/(\d+)\s+# ) { my $date = join( '', ( $3, $1, $2) ); if ( ! $file_handles{$date} ) { open($file_handles{$date}, ">>", "$output_dir/$date") +or die "Could not open $date for writing: $!\n"; print { $file_handles{$date} } "$_\n"; } elsif ( defined $file_handles{$date} ) { print { $file_handles{$date} } "$_\n"; } } }


    agent_smith
      This is quite the same technique that chaos_cat suggested before, just a bit more inefficient, since
      print { $file_handles{$date} } "$_\n";
      can be put outside the conditional opening of the file.