The OPer seems to want to control one-time printing/logging from outside the package/module, so that approach would not seem to do the trick. (Also, I don't know what the
+ in front of the
__FILE__ is supposed to do.)
One can play around with caller to get a general solution closer to what I think the OPer wants:
# LogOnce.pm
package LogOnce;
use warnings;
use strict;
our @ISA = qw(Exporter);
use Exporter;
our @EXPORT = ();
our @EXPORT_OK = qw(log1);
my %once;
sub log1 {
my $signature = join '|', caller;
return if exists $once{$signature};
return $once{$signature} = print __PACKAGE__, ": @_ \n";
}
1;
# LogOnce.t
use warnings;
use strict;
use LogOnce qw(log1);
MAIN: {
log1('this should print', __FILE__, __LINE__);
t_log1_1('this should print');
t_log1_1('this should NOT print');
log1('this should print', __FILE__, __LINE__);
t_log1_2('this should print');
t_log1_2('this should NOT print');
log1('this should print', __FILE__, __LINE__);
}
sub t_log1_1 {
log1('called at', __FILE__, __LINE__, ':', @_);
log1('called at', __FILE__, __LINE__, ':', @_);
}
sub t_log1_2 {
t_log1_1('should NOT print', __FILE__, __LINE__);
log1('called at', __FILE__, __LINE__, ':', @_);
log1('called at', __FILE__, __LINE__, ':', @_);
t_log1_1('should NOT print', __FILE__, __LINE__);
}