in reply to Dynamically Creating a Code reference
Here is your solution, with superficial code omitted.
There is a closure over the initial value of $var (which can also
be set with eval, barring errors). The print statements
demonstrate that regardless on how you call the sub,
it always acts on its initial closured value.
my $profile; { my $var = eval '33'; # or just my $var = 33 # or my $var = setme_at_runtime(); $profile = { my_name => sub { ($var =~ /\d+/) ? 1: return } }; } my $fun = $profile->{my_name}; print $profile->{my_name}->( ); print $profile->{my_name}->('a'); print $profile->{my_name}->( 22 );
|
---|
In Section
Seekers of Perl Wisdom