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

In reply to Re^2: Creating subroutines on the fly by belden
in thread Creating subroutines on the fly by pander

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.