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

Hello guruz, jedis and the rest of you perl dudes.

Today my brain is concentrating on writing automatic code by using sql statements and hashes. I wanted to know if this is possible:

1. I have an sql table with some variables defined.
2. I need to create a hash with all the variables as keys and the values should be subroutines.

So, without SQL the declaring part should be the following:

my $var = sub { MOD->SUB($param); }

With SQL I have this

my $hash; while(my $r = $sth->fetchrow_hashref) { $hash->{$r->{'var'}} = sub { $r->{'mod'}->$r->{'sub'}($r->{'params'} +); } }

Is there any way to accomplish that?


Thanks!

Replies are listed 'Best First'.
Re: Writing automatic code from sql
by Corion (Patriarch) on Oct 11, 2011 at 10:22 UTC

    I don't understand what you want, and your code helps little. Maybe seeing the table definition and the table contents would help.

    I interpret your task to wanting to run code that is stored as a string in the database. Does DBIx::VersionedSubs sound like a solution to your problem?

      What I want is to create calls to defined packages or subs from strings defined in an sql table.
      Given three varchar columns : PACKAGE, SUB, PARAMS I want to create the following code
      PACKAGE->SUB(PARAMS);

        So just use

        use strict; my ($package,$sub,@params) = ("MyPackage","hello_world",'Hello','World +'); $package->$sub(@params); package MyPackage; use strict; sub hello_world { my ($self,@args)=@_; print "In My Package: $_\n" for @args; };