You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Wes Cravens <we...@cortex-it.com> on 2003/04/24 16:55:27 UTC

Dynamic anonymous subroutines

I would like to call a Module::Routine($argument) using variables.

I have tried things like

eval "${module}::${routine}('info')";

Which works however eval "${module}::${routine}($info)"; will not...

I get similar result with anonymous subs such as

my $coderef = sub{$module."::".$routine};
&$coderef("info");

or better examples there of.

I am using this so that a control module on my website will read the URI
and decide what Module::Routine pair to call.  I want this as generic as
possible so that third parties can contribute to the site but the core
does not need to know they exist.

Any advice greatly appreciated.

Regards,

Wes


Re: Dynamic anonymous subroutines

Posted by Richard Clarke <ch...@stud.man.ac.uk>.
Wes,

The following has always worked fine for me.

    $module = "Some::Module";
    $method = 'sum_method';
    @args = qw/1 2 3/;
    $module->$method(@args);

Richard.
 
    

----- Original Message ----- 
From: "Wes Cravens" <we...@cortex-it.com>
To: <mo...@perl.apache.org>
Sent: Thursday, April 24, 2003 3:55 PM
Subject: Dynamic anonymous subroutines


> I would like to call a Module::Routine($argument) using variables.
> 
> I have tried things like
> 
> eval "${module}::${routine}('info')";
> 
> Which works however eval "${module}::${routine}($info)"; will not...
> 
> I get similar result with anonymous subs such as
> 
> my $coderef = sub{$module."::".$routine};
> &$coderef("info");
> 
> or better examples there of.
> 
> I am using this so that a control module on my website will read the URI
> and decide what Module::Routine pair to call.  I want this as generic as
> possible so that third parties can contribute to the site but the core
> does not need to know they exist.
> 
> Any advice greatly appreciated.
> 
> Regards,
> 
> Wes
> 
> 
> 

Re: Dynamic anonymous subroutines

Posted by Perrin Harkins <pe...@elem.com>.
Wes Cravens wrote:
> I am using this so that a control module on my website will read the URI
> and decide what Module::Routine pair to call.  I want this as generic as
> possible so that third parties can contribute to the site but the core
> does not need to know they exist.

This is already available in a CPAN module called Apache::Dispatch. 
Check it out and see if meets your needs.

- Perrin



RE: Dynamic anonymous subroutines

Posted by Wes Cravens <we...@cortex-it.com>.
> > I would like to call a Module::Routine($argument) using variables.
> >
> > I have tried things like
> >
> > eval "${module}::${routine}('info')";

What I really wanted was this

$coderef = eval "\\&${module}::${routine}"

&$coderef{$arg1, $arg2, ...};

Works a treat!

Thanks for other input but this is exactly what is needed.  Others
mentioned Apache::Dispatch.
I can't at the moment recall why but it was disregarded early on in the
planning.

Cheers,

Wes


> >
> > Which works however eval "${module}::${routine}($info)"; will not...
>
> What works for me, if you want to figure out what to do based on the
> URI that was called on:
>
> Assuming that your module is called MyModule, is an object,
> and has routines named after the first bit of the URI (i.e.
> /foo/bar) would
> call your routine as MyModule->foo with 'bar' as only
> argument - make sure your
> module has an AUTOLOAD function to deal with unknown routines
> (uri's) and handle
> those appropriately:
>
> sub handler {
>     my $r=shift;
>     my $module=MyModule->new();
>     my @args=split(/\//, $r->uri());
>     my $cmd=shift(@args);
>
>     $module->$cmd(@args);
> }
>
> It's not the prettiest way to do it, but it's functional.. at least
> in my case.
>
> --
> Bernhard van Staveren   -   madcat(at)ghostfield.com
> GhostField Internet     -   http://www.ghostfield.com/
> "A witty saying proves nothing, but damn it's funny!" - me, 1998


Re: Dynamic anonymous subroutines

Posted by Bernhard van Staveren <ma...@ghostfield.com>.
On Thu, 24 Apr 2003, Wes Cravens wrote:
 
> I would like to call a Module::Routine($argument) using variables.
> 
> I have tried things like
> 
> eval "${module}::${routine}('info')";
> 
> Which works however eval "${module}::${routine}($info)"; will not...

What works for me, if you want to figure out what to do based on the
URI that was called on: 

Assuming that your module is called MyModule, is an object,
and has routines named after the first bit of the URI (i.e. /foo/bar) would
call your routine as MyModule->foo with 'bar' as only argument - make sure your
module has an AUTOLOAD function to deal with unknown routines (uri's) and handle
those appropriately:

sub handler {
    my $r=shift;
    my $module=MyModule->new();
    my @args=split(/\//, $r->uri());
    my $cmd=shift(@args);
    
    $module->$cmd(@args);
}

It's not the prettiest way to do it, but it's functional.. at least
in my case.

-- 
Bernhard van Staveren   -   madcat(at)ghostfield.com
GhostField Internet     -   http://www.ghostfield.com/
"A witty saying proves nothing, but damn it's funny!" - me, 1998 

Re: Dynamic anonymous subroutines

Posted by Ged Haywood <ge...@www2.jubileegroup.co.uk>.
Hi there,

On Thu, 24 Apr 2003, Wes Cravens wrote:

> I would like to call a Module::Routine($argument) using variables.
> 
> I have tried things like
> 
> eval "${module}::${routine}('info')";
> 
> Which works however eval "${module}::${routine}($info)"; will not...

Try playing around with

print STDERR "${module}::${routine}($info)";

etc. and you may see where it's going wrong.

Check out also the debugging section of the guide.

73,
Ged.