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

Re^2: Creating subroutines on the fly

by belden (Friar)
on Feb 16, 2005 at 23:23 UTC ( [id://431786]=note: print w/replies, xml ) Need Help??


in reply to Re: Creating subroutines on the fly
in thread Creating subroutines on the fly

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-24 01:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found