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

Hi, I need to create a program, configure its behavior and than save its state into a database. To do that, I have a library (.pm) with some functions, and I want to save into the database the name of the functions that shall be called when I run my program the next time.
Can I save the name of the functions into the database and then, (on load) call the correspondent function using its name?

For example:
Considering that I have the following librabry (
@EXPORT = qw( funtion1 function2 function3 funtion4)
and that I read from D.B. fhis two strings: 'funtion1' and 'funtion4', how can I execute function1 and function4 from

Thanks in advance

Replies are listed 'Best First'.
Re: Access functions by name
by grep (Monsignor) on Jun 15, 2009 at 20:38 UTC
    Create a real object and then you call your methods by the method name stored in the DB;
    # This is a code fragment and assumes a DBI statement handle $sth # that retrieves the appropriate records my $fs = FunctionSet->new(); while ( my $row = $sth->fetchrow_hashref ) { my $method_name = $row{method_name}; my $arg = $row{arg}; $fs->$method_name($arg); }
    One dead unjugged rabbit fish later...
Re: Access functions by name
by Your Mother (Archbishop) on Jun 15, 2009 at 21:55 UTC

    This is not meant as advice, just an option. The no strict refs approach JavaFan showed has always bothered me. This works without turning strict off. It has no other advantage I can think of though and might be harder to follow for some devs. Hence, not advice, just discussion.

    use strict; my $func_ref = \&{'function1'}; $func_ref->( 1 .. 3 ); # Or... &$func_ref( 1 .. 3 ); sub function1{ print "I gots: ", join("+", @_), $/ }
Re: Access functions by name
by JavaFan (Canon) on Jun 15, 2009 at 20:06 UTC
    my $func_name = 'function1'; no strict 'refs'; &$func_name(arg1, arg2, arg3);
Re: Access functions by name
by llancet (Friar) on Jun 16, 2009 at 07:29 UTC
    If all of the functions are sure, you may call the functions by names in this way:
    my %func_map={ "function1" => \&function1, "function2" => \&function2, # ..... }; # while you got a name, call the corresponding function my $name=some_way_you_got_it(); &{$func_map{$name}}($your_param1,$your_param2);
Re: Access functions by name
by Arunbear (Prior) on Jun 16, 2009 at 11:10 UTC
    Either use UNIVERSAL's "can" method, or look up the name in the symbol table hash %FunctionsSet:: (see perlmod) e.g.
    use strict; use warnings; package FunctionsSet; sub function1 { warn "This is function1" } sub function2 { warn "This is function2" } sub function3 { warn "This is function3" } sub function4 { warn "This is function4" } package main; my @names = qw/function1 function4/; foreach my $func (@names) { FunctionsSet->can($func)->(); } my @more_names = qw/function2 function3/; foreach my $func (@more_names) { $FunctionsSet::{$func}->(); }