in reply to Re: Writing automatic code from sql
in thread Writing automatic code from sql

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);

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

    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; };
      Here's the code that works:

      my %h; while(my $r = $st->fetchrow_hashref()) { my $mod = $r->{'Module'}; my $sub = $r->{'Subroutine'}; my $par = $r->{'Params'}; $h{$r->{'VarName'}} = sub { $mod->$sub(eval "$par"); }; }
      You've got my +! Thanks again.

        This is basically what DBIx::VersionedSubs::Hash does, except it also allows you to dynamically load newer versions from the database.