如果我有一个打开文件的子例程,那么确保它仅在第一次调用子例程时打开它的最佳方法是什么?我有这个但不确定它是否是最佳实践:
{
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++;
}
}