in reply to Re: Functions that know which package they've been exported to.
in thread Functions that know which package they've been exported to.
No, caller in the logMessage function itself does something different. Witness:
{ package My::Logger; sub import { my $PACKAGE = caller; no strict 'refs'; *{"$PACKAGE\::log_message"} = sub { my ($message) = @_; print STDERR "$PACKAGE - $message"; }; } } { package My::Utils; My::Logger->import; } { package main; # outputs "My::Utils - Hello", not "main - Hello" My::Utils::log_message("Hello"); }
The log_message function outputs what package it has been installled into, not called from.
While this is rarely useful, it does occasionally have uses. Object::Stash uses something along these lines.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Functions that know which package they've been exported to.
by BrowserUk (Patriarch) on Jan 24, 2012 at 09:48 UTC |