sub x {
my $func = do { no strict 'refs'; *{caller() . '::_x'}{CODE} };
die() unless defined $func;
&$func;
# or &$func(...);
}
####
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)
##
##
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)