variation on 'calling subroutine via reference'?

A

Alison Bowes

I posted this on comp.lang.perl then ran across another post saying that
that newsgroup is "defunct" and posts should be made here instead. Sorry if
you're seeing this twice!

I know that if $subname contains the name of the subroutine I want to call,
I can call that subroutine via

&$subname;

However, my subroutine names are in an array, so if I want to call
$subname[3] I've tried

&$subname[3];

But, that doesn't work, and I'm not having much luck finding such an
example. If I'm doing something truly numbskulled, I do apologize!

I do realize I could go this route:
$this_sub = $subname[3];
&$this_sub;

But, that just seems so clunky and unnecessary, no?

TIA,
Alison
 
T

Tassilo v. Parseval

Also sprach Alison Bowes:
I posted this on comp.lang.perl then ran across another post saying that
that newsgroup is "defunct" and posts should be made here instead. Sorry if
you're seeing this twice!

I know that if $subname contains the name of the subroutine I want to call,
I can call that subroutine via

&$subname;

However, my subroutine names are in an array, so if I want to call
$subname[3] I've tried

&$subname[3];

But, that doesn't work, and I'm not having much luck finding such an
example. If I'm doing something truly numbskulled, I do apologize!

You have to bear in mind that the &$REF syntax is really a short-cut.
An explicit dereference will put the reference into curlies:

${ $REF }
%{ $REF }
...
I do realize I could go this route:
$this_sub = $subname[3];
&$this_sub;

But, that just seems so clunky and unnecessary, no?

It does. Adding curlies as mentioned above makes this intermediate step
unnecessary:

&{ $subname[3] };

Tassilo
 
G

Gunnar Hjalmarsson

Alison said:
I know that if $subname contains the name of the subroutine I want to call,
I can call that subroutine via

&$subname;

Really? On my computer, this code:

use strict;
sub myfunction { print "Hello\n" }
my $subname = 'myfunction';
&$subname;

outputs:
Can't use string ("myfunction") as a subroutine ref while "strict refs"
in use

But this works fine:

my $subref = \&myfunction;
&$subref;
However, my subroutine names are in an array, so if I want to call
$subname[3] I've tried

&$subname[3];

If the fourth element contains a *code reference* to the subroutine, this:

&{ $subname[3] };

or this:

$subname[3]->();

should work.

Please study the posting guidelines for this group:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Among other useful things, they emphasize the importance of using strict
and warnings when developing Perl code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top