[VPM] passing parameters to sub ref then execute

Darren Duncan darren at darrenduncan.net
Thu Aug 7 17:42:33 PDT 2008


Peter Scott wrote:
> At 02:21 PM 8/7/2008, Darren Duncan wrote:
>> Jer A wrote:
>>> if this is how to exec a subroutine ref, without params .eg $o = &$c;
>>> how do I pass parameters to it.
>>> thanks in advance for your help.
>>> Jeremy A
>>
>> I would spell that like this:
>>
>>   $o = &{$c}();
>>
>> And then put the arguments between the parenthesis as usual.
> 
> I write
> 
>         $o = $c->();
> 
> in both cases.  Either there are arguments between the parentheses or 
> not.  Preferring the arrow operator for dereferencing is a bestpractice 
> IMHO.

Yes, that works too.

Come to think of it, my example was derived from a somewhat different 
situation, when I wanted to invoke a package-qualified routine that wasn't 
a method.

Something closer to my source example is:

   my $result = &{$mypkgname->can( 'myfunc' )}(...);

This is for when I wanted the semantics of "MyPkg::myfunc(...)" but the 
name of MyPkg was in a variable.

So my example was mis-applied here; when $c is already a subroutine ref, 
then Peter's syntax is better.

-- Darren Duncan


More information about the Victoria-pm mailing list