Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Why do I get a "used only once" warning here?

by AnomalousMonk (Archbishop)
on Mar 13, 2009 at 22:13 UTC ( [id://750532]=note: print w/replies, xml ) Need Help??


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?

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__); }

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
    The OPer seems to want to control one-time printing/logging from outside the package/module

    If one really wants a global in some other package, then...

    package MyModule; use strict; use warnings; sub f { $MyApp::OneTime{+__FILE__.' '.__LINE__} ||= (print("logging\n"), 1 +); return(); } 1;
Re^3: Why do I get a "used only once" warning here?
by rovf (Priest) on Mar 16, 2009 at 08:50 UTC
    I don't know what the + in front of the __FILE__ is supposed to do

    In the current form, it has no meaning. It is a leftover from an earlier version, which read something like:

    $MyApp::once{+__LINE__} ....
    and so the + stayed there and when posting my problem, I copied it without much thinking. If you have only __LINE__ as key, the + is needed because __LINE__ is a bareword.

    -- 
    Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://750532]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found