sub test {
BEGIN {warn "---- function-scope\n"};
use Module;
func("function-scope");
# no Module; # uncommenting will cause a compile-time error
}
test();
func('filescope');
####
---- function-scope
* importing from Module
running func(function-scope) at d:/exp/t_lexical_unimport.pl line 13.
running func(filescope) at d:/exp/t_lexical_unimport.pl line 19.
####
my $caller_pack = caller;
# --- modulino
# run test script if Module not required
unless (caller) {
use English;
my $run =`$EXECUTABLE_NAME t_lexical_unimport.pl`;
exit;
}
use strict;
use warnings;
package Module;
use Carp;
sub import {
warn "* importing from ",__PACKAGE__,"\n";
no strict 'refs';
*{"${caller_pack}::func"} = \&func;
}
sub unimport {
warn "* unimporting from ",__PACKAGE__,"\n";
no strict 'refs';
delete ${"${caller_pack}::"}{func}; # dirty: deletes whole glob
}
# ---------- to be exported
sub func {
carp "running func(@_)";
}
1;