Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Dynamically Creating a Code reference

by ioannis (Monsignor)
on Jun 16, 2006 at 00:10 UTC ( #555660=note: print w/replies, xml ) Need Help??

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 );

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://555660]
[Corion]: choroba: I admit I've thought about it ;) But I think I'll sooner hack up the Pg parser library to spit out a good parse tree and then hack that up to create Perl code :)
[Corion]: But back when I last looked at the VM of SQLite it seemed way to basic to me to implement anything in it....

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2018-03-22 12:45 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (274 votes). Check out past polls.