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__); }
In reply to Re^2: Why do I get a "used only once" warning here?
by AnomalousMonk
in thread Why do I get a "used only once" warning here?
by rovf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |