rrwo has asked for the wisdom of the Perl Monks concerning the following question:
I have a hash where the keys are method names and the values have stuff I want the methods to do... my module dynamically generates these methods in the BEGIN block, and I would like to export these methods:
require Exporter; our @ISA = qw( Exporter ); our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); my %METHODS = ( foo => 'bar', bo => 'baz', ); our @EXPORT = (%keys %METHODS); BEGIN { foreach my $method (keys %METHODS) { no strict 'refs'; *$methods = sub { return $METHODS{ $method }; }; } }
The dynamic methods are generated with no problem, but it seems @EXPORT doesn't contain these values. I tried to modify the begin block as below but it still doesn't work:
BEGIN { foreach my $method (keys %METHODS) { push @EXPORT, $method; no strict 'refs'; *$methods = sub { return $METHODS{ $method }; }; } }
I thought the 'import' function was run after the BEGIN block, so @EXPORT should have these values. Obviously I'm wrong. An explanation and advice would help...
|
|---|