How to open a file only if it is not already open in Perl?

If I have a subroutine that opens a file, what is the best way to ensure that it opens only the first time the subroutine is called? I have this, but not sure its best practice:

{
my $count = 0;
sub log_msg {
    my ($msg,$name) = @_;

    if ($count == 0) {
        my $log_file_name = "/tmp/" . $name;
        open my $log_fh,">",$log_file_name or  croak "couldn't open $log_file_name : $!";
        print $log_fh "$timestamp: created and opened $log_file_name\n";
    }
    $count++;
    }
}
+5
source share
4 answers

Sounds like a good reason to use a state variable. Keep file descriptors in a persistent hash.

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

sub log_msg {
  state %fh;
  my ($msg, $name) = @_;

  unless ($fh{$name}) {
    warn "Opening $name\n";
    open $fh{$name}, '>', $name or die $!;
    print {$fh{$name}} scalar localtime, " Opened file\n";
  }

  print {$fh{$name}} $msg, "\n";
}

log_msg('Message1', 'first.log');
log_msg('Message2', 'first.log');
log_msg('MessageA', 'second.log');
log_msg('MessageB', 'second.log');

Note the extra set of braces around file descriptors in the print call. This is because printing is a bit picky about what you can use filehandle as an argument.

+8
source

Log:: Log4perl, , .

, , " Perl". , , .

, . . , , ( , ):

 sub log_msg {
      my( $self, $msg, $name ) = @_;

      print { $self->get_fh_by_name( $name ) } $msg;
      }

 BEGIN { # to define variables before the subroutine
 my %log_fhs;
 sub get_fh_by_name {
     my( $self, $name ) = @_;

     return $log_fhs{$name} if defined $log_fhs{$name};

     open my $log_fh, catdir( $base_dir, $name ) or croak "...";
     print $logfh ...

     $log_fhs{$name} = $log_fh;
     }
 }
+3

, , $count++ if $count=1. $count $file_opened_flag - . , .

0

I don’t think that there is something wrong with using per se, but if there really is only one file to track, why just save $log_fhin closing and use if(!$log_fh->opened())count instead?

0
source

All Articles