in reply to Creating subroutines on the fly

Sure, you can store perl code as a string in a file or a database. Here is an example of how to turn it into a sub.

$code = "print 'Hello World.'"; # This may have come for your DB $sub = eval "sub { $code }"; # use eval and string interpolation to co +nvert it into an anon. sub $sub->(); # Execute the sub; # To pass arguments to the sub, do this: $sub->($a1, $a2); #etc # You can capture the return value too $ret = $sub->();

Update: I almost forgot to warn you: be very careful with who has access to the string version of the perl code. Obviously, if that data came from an untrusted source, they could destroy your system!

Ted Young

($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

Replies are listed 'Best First'.
Re^2: Creating subroutines on the fly
by cowboy (Friar) on Feb 16, 2005 at 16:39 UTC
    Dynamic code is also very hard to debug. I would recommend something like the following, to help you find out when/where it has errors.
    $code = "print 'Hello World.'"; $sub = eval "sub { $code }"; unless ($sub) { # ack! it failed to eval # lets leave a note about the code, and the error # so we can debug this. die sprintf('CODE: %s\n\nERROR: %s\n\n', $code, $@); }
Re^2: Creating subroutines on the fly
by belden (Friar) on Feb 16, 2005 at 23:23 UTC

    If you want to name your subroutines, then just stick in a name at the right spot.

    sub make_sub { my ( $sub_name, $code ) = @_ ; local $@; eval "sub $sub_name { $code }" ; die "make_sub $sub_name: $@\n" if $@; } make_sub( say_hello => 'print "Hello @_\n"' ); say_hello( 'World' ); __END__ Hello World

    Note that this will throw wierd warnings if you try to create two subs with the same name. You can work around those....

    ...but chances are that you don't really need to use eval for whatever you're trying to do. You can probably get away with writing a module which contains all the functions you want to store in your database, and then just store the function names in the database instead. Have your program fetch the right name from the database, then then call it.

    Here's your stored functions.

    package MyFunctions; # save this as MyFunctions.pm sub say_hello { shift ; # discard class name: perldoc perlboot print "Hello @_\n" } sub list_dir { shift ; # discard class name require Cwd; my $owd = Cwd::cwd(); my $dir = shift; chdir $dir or die "chdir $dir: $!\n"; print $_ . "\n" while <*> ; chdir $owd; } 1;

    And here's your program. Here, rather than fetching function names and function arguments from a database, we'll just get it from the user.

    #!/usr/bin/perl # save this as program.pl use strict; use warnings; use MyFunctions; print "action> " ; chomp( my $action = <STDIN> ); print "args> "; chomp( my $arg = <STDIN> ); MyFunctions->$action( $arg );

    And here's how it might look when you run it

    $ perl program.pl action> list_dir args> /tmp foo bar baz

    The advantage of storing all your functions in a file and calling them as class methods (i.e. MyFunction->say_hello() rather than MyFunction::say_hello()) is that you have control over what your program does. If your program fetches code from a database, or gets it from the user, then you no longer have control over your program's behavior.

    Belden