http://www.perlmonks.org?node_id=725804


in reply to Re^3: goto &sub and local question
in thread goto &sub and local question

Why? What about your usage requires you to call a subroutine with goto?

Well, I wanted to spare you these tedious details, because I didn't consider them important to know, but here we go:

The module these functions are in is a mediation layer to a logging module (Log::Log4perl actually). Originally, the client modules used, for instance, Log4perl functions such as DEBUG(...) or ERROR(...) to generate the logs.

This system is now about to be modified in that DEBUG, ERROR etc. should first invoke some of our own code to do some preliminary work, and then pass on the DEBUG, ERROR etc. of the Log4perl module. In addition, my own version of these routines to do first some special processing, then some general processing (which is common to all type of traces), and then pass control to Log4perl. Basically, the information flow is as follows:

  1. User calls DEBUG(), ERROR(), INFO() etc., invoking my own DEBUG/ERROR/INFO routines.
  2. My DEBUG does some actions specific to the debug case.
  3. Next, the general code (common for DEBUG, ERROR etc.) is executed.
  4. Finally, Log::Log4perl::DEBUG/ERROR/INFO etc. is run
If I would do this with normal function calls, i.e.
package MyLogging; ... sub DEBUG { ... general(\Log::Log4perl::DEBUG,@_); } sub general { my $logger=shift; ... &$logger(@_); }
the Log4perl logging routines would report MyLogging::general as caller, instead of the original call site of DEBUG().

Maybe there is a callback mechanism in Log4perl which I could use; the documentation of Log4perl is pretty big and I thought that my problem is so easy that I could do it simply by using (kind of):

package MyLogging; ... sub DEBUG { ... unshift @_,\Log::Log4perl::DEBUG; goto &general; } sub general { my $logger=shift; ... goto &$logger; }

Actually, it *does* work that way, and I believe that since these functions are small and co-operate closely, it is also easy to understand. Still, I am not absolutely happy with the solution either. If I find time, I should maybe take a few hours to wade through the Log4perl docs to get ideas for other solutions.

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re^5: goto &sub and local question
by dragonchild (Archbishop) on Dec 01, 2008 at 17:34 UTC
    Sounds like you want aspects, specifically the pre-run aspect. Take a look at AOP or (preferable) Class::MOP. Someone else wrote one a few months ago that only does pre-run hooks, but I didn't like the author's attitude.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      I was not aware that Aspect Oriented Programming is available in Perl! I will have a look at it - thanks a lot for your suggestions.

      -- 
      Ronald Fischer <ynnor@mm.st>