Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by agent_smith (Scribe)
on Jan 16, 2008 at 16:20 UTC ( [id://662706]=note: print w/replies, xml ) Need Help??


in reply to Re: 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?

Thanks for the info, but doesn't this still open possibly unnecessary file handles? Maybe I should elaborate, and perhaps ask forgiveness for not being detailed enough.
while(<DATA>) { chomp; if ($_ =~ m#^(\d+)/(\d+)/(\d+)\s+.*admin2;user) { open(FH$3, ">$3.$2.$1"); print "$_\n" FH$3; } }


The problem with the above, is FH$3 may already be open, and I don't want to try to reopen it.

And the problem with my first example is that it may open files for which I have no data, for example there was no data on the 15th, but I had opened the file anyway ( resulting in an un-needed 0 length file.

Thanks again, for the help

agent_smith

Replies are listed 'Best First'.
Re^3: How can you check to see if a file handle is already open?
by moritz (Cardinal) on Jan 16, 2008 at 16:46 UTC
    I could have made it clearer ;-)

    You can actually avoid opening all the file handles with a small sub:

    my %handles; sub handle_for_name { my $f = shift; if ($handles{$f}){ return $handles{$f}; } else { open my $h, '<', $f or die "Can't open file '$f': $!"; $hanldes{$f} = $h; return $h; } }

    And then whenever you need a file handle you just call that function that opens the file if it's not already opened.

    You can make that sub even smaller with a neat little trick:

    use Memoize; memoize("handle_for_name"); sub handle_for_name { my $f = shift; open my $h, '<', $f or die "Can't open file '$f': $!"; return $h; }

    That's magic, eh?

    To answer your original question: if you have a file handle in a lexical variable $h, you can use if ($fh){ print "file opened\n"}

Re^3: How can you check to see if a file handle is already open?
by alexm (Chaplain) on Jan 16, 2008 at 17:58 UTC
    moritz's approach is the same I had in mind, but there's something else that you should care about: if the dataset contains dates for different months and years, you could run out of resources quite easily (i.e. too many open files).

    In these cases, FileCache is your friend ;-)

Re^3: How can you check to see if a file handle is already open?
by mifflin (Curate) on Jan 16, 2008 at 19:58 UTC
    Why not just use the IO::Handle package?

    Use the opened method. It will tell you if a file handle is opened and ready.

    try the following code...

    use strict; use warnings; use IO::Handle; open my $fh, '>', 'file' or die $!; test_fh($fh); close $fh or die $!; test_fh($fh); sub test_fh { my ($fh) = @_; if ($fh->opened()) { print "fh is opened and ready\n"; } else { print "fh is closed\n"; } return; }
Re^3: How can you check to see if a file handle is already open?
by Limbic~Region (Chancellor) on Jan 16, 2008 at 20:02 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://662706]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-23 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found