in reply to Re: Why do I get a "used only once" warning here?
in thread Why do I get a "used only once" warning here?
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__); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Why do I get a "used only once" warning here?
by ig (Vicar) on Mar 13, 2009 at 22:57 UTC | |
|
Re^3: Why do I get a "used only once" warning here?
by rovf (Priest) on Mar 16, 2009 at 08:50 UTC |