variation on 'calling subroutine via reference'?

Discussion in 'Perl Misc' started by Alison Bowes, Feb 18, 2005.

  1. Alison Bowes

    Alison Bowes Guest

    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
     
    Alison Bowes, Feb 18, 2005
    #1
    1. Advertising

  2. 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
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Feb 18, 2005
    #2
    1. Advertising

  3. Alison Bowes wrote:
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 18, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alison Bowes
    Replies:
    0
    Views:
    487
    Alison Bowes
    Feb 18, 2005
  2. Erlend Andreas Garberg

    Travelling salesman variation in python

    Erlend Andreas Garberg, Apr 1, 2004, in forum: Python
    Replies:
    20
    Views:
    4,232
    G√ľnter Jantzen
    Apr 2, 2004
  3. j0mbolar

    opinions on logical OR variation

    j0mbolar, Sep 28, 2004, in forum: C Programming
    Replies:
    3
    Views:
    319
    Tim Rentsch
    Sep 29, 2004
  4. John W. Burns

    calling subroutine via hash

    John W. Burns, Jan 3, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    110
    John W. Burns
    Jan 5, 2006
  5. king
    Replies:
    5
    Views:
    210
Loading...

Share This Page