Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Dynamic Code?

by kael (Monk)
on Aug 25, 2000 at 05:55 UTC ( [id://29572]=perlquestion: print w/replies, xml ) Need Help??

kael has asked for the wisdom of the Perl Monks concerning the following question:

Ok, is there a way to do this without putting my code through a meat grinder? I want to be able to say like
$a='if ($joe) then {somesub(arg1,arg2)}';

Then later have $a execute as if it were normal code.(Hope that makes sense)
Any suggestions on how to do it would be much apreciated.

Replies are listed 'Best First'.
Re: Dynamic Code?
by btrott (Parson) on Aug 25, 2000 at 06:00 UTC
    You can do that... but that's not normal Perl code. :) There's no "then" statement in Perl, and you'll need to do something with those arguments.

    The secret is the string form of eval:

    my $a = 'somesub($arg1, $arg2) if $joe'; eval $a;
      it was 3 am give me a break :) though I haven't used then since my TP days like 5 years ago...
      Thanx for the help everyone. now I get to fiddle and make it work
Re: Dynamic Code?
by gaspodethewonderdog (Monk) on Aug 25, 2000 at 17:16 UTC
    btrott and mikkoh both present the two best solutions to solving the problem. It all depends on how dynamic you want your code to be. If you have several predefined functions and $a can only be a handful of them I would go for setting $a to the function and then calling it by &$a because this will get you the fastest execution time.

    However, if you want $a to be truly dynamic you can use eval to execute the contents of $a, but keep in mind that each time you run it perl has to recompile the eval.

    Essentially if you want perl to only compile the function once and you have a couple predefined functions I think setting $a to a function is the way to go, otherwise I guess taking the performance hit from eval and recompiling the code every time $a is executed would be the way to go.

    Hopefully this helps give you an idea as to why you would want to use either of the two ways.

    Otherwise, if you're looking to do something *really* wackey you could use evals to create static functions ala

    eval '$a = sub { if($joe){somesub($arg1,$arg2);} }'
    This way you can get the performance boost of executing $a through &$a but still have dynamic code from eval.
Re: Dynamic Code?
by mikkoh (Beadle) on Aug 25, 2000 at 11:41 UTC
    I'm not sure if this is what you're after, but I think what you need is called a closure..

    sub func{ return sub { if($joe){somesub($arg1,$arg2);} } } $a=&func(); # do stuff.. # call the function &$a;
    I didn't test this, just wrote it. See perlman:perlref for more.
    //mikkoH
      Dang, too little coffee today :)
      That was actually just a reference to an anonymous sub I just described, not a real closure..

      $a = sub { if($joe){somesub($arg1,$arg2);} }

      would do just the same..
      //mikkoH

        Ah, but it could be <grin>, its just a question of when you want $arg1 and $arg2 defined... maybe kael wanted the args declared when $a is declared....
        sub func{ die 'func takes 2 args' unless 2 == @_; my ( $arg1, $arg2 ) = @_; return sub { somesub($arg1,$arg2) if($joe) } } $a=&func( $somearg, $someotherarg ); # do stuff.. # call the function &$a;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://29572]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2024-04-23 10:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found