I am trying to write a pragma to define a bunch of constants, for example:
use many::constant
one_constant => 1,
other_constant => 2,
;
The relevant part of my import looks like
package many::constant;
use strict;
use warnings;
sub import {
my ($class, @constants) = @_;
my $caller_nms = do {
no strict 'refs';
\%{caller.'::'}
};
while (my ($name, $value) = splice @constants, 0, 2) {
*{$caller_nms->{$name}} = sub () { $value };
}
}
I expect stash $caller_nmsto automatically revive when assigned in this way, but I get the error message “Unable to use undefined value as a symbol reference”. Is there a way to get this task to work as I expect? I ended up changing the assignment:
my $caller_glob = do {
no strict 'refs';
\*{caller.'::'.$name}
};
*$caller_glob = sub () { $value };
but for me it is less elegant.
source
share