in reply to Why do I get a "used only once" warning here?

You seem to be spreading functionality that can be contained within the logging function. Why not:
use warning; use strict; { my %log_seen; sub log_info { # do not continue if done before return if $log_seen{(caller(1))[3]}++; warn(@_, "\n"); } } sub f { log_info('some information'); print "called f()\n"; } f(); f(); __END__ some information called f() called f()

Replies are listed 'Best First'.
Re^2: Why do I get a "used only once" warning here?
by rovf (Priest) on Mar 16, 2009 at 08:55 UTC

    The reason why I haven't put it into the log package is that this "log once" logic is supposed to be done only for a few selected logs. If I put it into the log package, I would have to write a new logging function, say log_once. Actually this is a serious alternative and I was thinking to do it, but when I started to implement this feature, I thought it would be easily possible too to have this logic implemented without changing/extending the logging package, then was trapped by the unexpected warning, and then my curiosity took over....

    -- 
    Ronald Fischer <ynnor@mm.st>
      Understood - you have to weigh the pros and cons of a log_once(). Just some thoughts:

      If you had a logging package that expected all other functions to be logged, except for a select few, and then for the select few you sprinkle some state variables around, doesn't that introduce some action-at-a-distance & maintenance difficulties in the far future?

      Granted, if there was just one or two state variables around, it would just be easy to remember, but wouldn't it also be that much harder to locate later?
        doesn't that introduce some action-at-a-distance & maintenance difficulties in the far future?

        I don't think so. At the point of logging, we know whether logging should be "once" or "always", and in all solutions proposed so far this would be clear from looking at the place where the logging occurs. From a programming point of view, I think a log_once() function would be easier to understand, though; OTOH, I expect that we might get diagnostics in the future (besides logging) which also should be executed only once, so I have not decided yet which way to go.

        At least the discussion in this thread provided me with enough insight that I now see what possibilities I have.

        -- 
        Ronald Fischer <ynnor@mm.st>