Like this?
sub x { my $func = do { no strict 'refs'; *{caller() . '::_x'}{CODE} }; die() unless defined $func; &$func; # or &$func(...); }
Test code:
package M; use strict; use warnings; BEGIN { our ISA 'Exporter'; our EXPORT_OK qw ( x ); require Exporter; *import = \&Exporter::import; } sub x { my $func = do { no strict 'refs'; *{caller() . '::_x'}{CODE} }; die() unless defined $func; &$func; # or &$func(...); } 1; ^^^ M.pm ^^^ vvv script.pl vvv use strict; use warnings; use M qw( x ); sub _x { print("main::_x(" . join(', ', @_) . ")\n"); } x('a', 'b'); __END__ outputs ======= main::_x(a, b)
But you know what, it would be much cleaner if you did:
package M; use strict; use warnings; BEGIN { our ISA 'Exporter'; our EXPORT_OK qw ( x ); require Exporter; *import = \&Exporter::import; } sub x { my $callback = shift(@_); &$callback; # or &$callback(...); } 1; ^^^ M.pm ^^^ vvv script.pl vvv use strict; use warnings; use M qw( x ); sub _x { print("main::_x(" . join(', ', @_) . ")\n"); } x(\&_x, 'a', 'b'); __END__ outputs ======= main::_x(a, b)
In reply to Re: Getting module to call importer's function
by ikegami
in thread Getting module to call importer's function
by jredburn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |