calling subroutine , name derived from variable

Discussion in 'Perl Misc' started by Madhu Ramachandran, Jan 6, 2006.

  1. all:

    I want to call subroutine, but the name of the subroutine itself is in a
    variable. I was able to do this. However, i also want to pass arguments ..
    not able to do this.
    eg: if $sub has the subroutine name, and $arg has the arg to the subroutine.
    How can i call this from my perl main program? i tried eval() and it worked
    if i dont have any arguments.

    #!/usr/local/bin/perl

    $method="aSub";
    $aa="\"bla\"";
    $mm = "$method" . "($aa)";
    print ("mm = $mm\n");
    $ret = eval($mm);
    print ("ret = $ret\n");
    $ret=eval($method);
    print ("ret = $ret\n");

    sub aSub()
    {
    my ($arg1) = @_;
    print ("inside aSub, arg1=$arg1\n");
    return 1;
    }
    ##### Output ######
    mm = aSub("bla")
    ret =
    inside aSub, arg1=
    ret = 1
    ##################

    My perl version: This is perl, v5.6.0 built for sun4-solaris

    any pointers?

    Thanks in advance.

    Madhu
     
    Madhu Ramachandran, Jan 6, 2006
    #1
    1. Advertising

  2. Madhu Ramachandran

    Matt Garrish Guest

    "Madhu Ramachandran" <> wrote in message
    news:dpkck1$g8b$...
    > all:
    >
    > I want to call subroutine, but the name of the subroutine itself is in a
    > variable. I was able to do this. However, i also want to pass arguments ..
    > not able to do this.


    You're looking for symbolic references, but it's not good practice to use
    them. For example:

    $subRef = 'this_sub';
    $arg1 = 'first argument';
    $arg2 = 'second argument';

    &{$subRef}($arg1, $arg2);

    sub this_sub {
    print "$_[0] : $_[1]\n";
    }


    It's better practice to use a hash as you won't break the strictures pragma
    that way, which should make your code easier to maintain:

    my %subRefs = ( this_sub => \&this_sub );

    my $subRef = 'this_sub';

    my $arg1 = 'first argument';
    my $arg2 = 'second argument';

    $subRefs{$subRef}($arg1, $arg2);

    sub this_sub {
    print "$_[0] : $_[1]\n";
    }

    Matt
     
    Matt Garrish, Jan 6, 2006
    #2
    1. Advertising

  3. Madhu Ramachandran wrote:
    > I want to call subroutine, but the name of the subroutine itself is in a
    > variable. I was able to do this. However, i also want to pass arguments ..


    my $method = 'aSub';
    my $aa = 'bla';
    my $subref = \&$method;
    my $ret = $subref->($aa);
    print "ret = $ret\n";

    sub aSub {
    my ($arg1) = @_;
    print "inside aSub, arg1=$arg1\n";
    return 1;
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 6, 2006
    #3
  4. Madhu Ramachandran

    robic0 Guest

    On Thu, 5 Jan 2006 19:42:03 -0500, "Matt Garrish" <> wrote:

    >
    >"Madhu Ramachandran" <> wrote in message
    >news:dpkck1$g8b$...
    >> all:
    >>
    >> I want to call subroutine, but the name of the subroutine itself is in a
    >> variable. I was able to do this. However, i also want to pass arguments ..
    >> not able to do this.

    >
    >You're looking for symbolic references, but it's not good practice to use
    >them. For example:
    >
    >$subRef = 'this_sub';
    >$arg1 = 'first argument';
    >$arg2 = 'second argument';
    >
    >&{$subRef}($arg1, $arg2);
    >
    >sub this_sub {
    > print "$_[0] : $_[1]\n";
    >}
    >
    >
    >It's better practice to use a hash as you won't break the strictures pragma
    >that way, which should make your code easier to maintain:
    >
    >my %subRefs = ( this_sub => \&this_sub );
    >
    >my $subRef = 'this_sub';
    >
    >my $arg1 = 'first argument';
    >my $arg2 = 'second argument';
    >
    >$subRefs{$subRef}($arg1, $arg2);
    >
    >sub this_sub {
    > print "$_[0] : $_[1]\n";
    >}
    >
    >Matt
    >


    This is bizzar, why would you use a hash of references to subs to call a subroutine?
    Any use for that at all? Somebody gonna pass you the name of a subroutine in a packet?

    The only "logical" use for an array of subroutines (or function pointers) is the "index",
    into it, which means the "name" of the handler is hidden.

    -robic0-
    actual subroutine reference can be called.
     
    robic0, Jan 6, 2006
    #4
  5. Gunnar Hjalmarsson wrote:
    > Madhu Ramachandran wrote:
    >> I want to call subroutine, but the name of the subroutine itself is in
    >> a variable. I was able to do this. However, i also want to pass
    >> arguments ..

    >
    > my $method = 'aSub';
    > my $aa = 'bla';
    > my $subref = \&$method;
    > my $ret = $subref->($aa);
    > print "ret = $ret\n";
    >
    > sub aSub {
    > my ($arg1) = @_;
    > print "inside aSub, arg1=$arg1\n";
    > return 1;
    > }


    The above solution 'works', but I'd better admit that it's actually a
    symref, even if it passes strict. Matt's solution was criticized by that
    robic0 character, which proves that Matt provided a better answer.

    Please disregard my code above.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 6, 2006
    #5
  6. robic0 wrote:
    > This is bizzar, why would you use a hash of references to subs to
    > call a subroutine? Any use for that at all?


    Oh yes, very common scenario in jump tables.

    Like in (pseudo-code)
    if ($op eq "add) then return $left + $right;
    if ($op eq "minus") then return $left - $right;
    if ($op eq "div") then return $left / $right;
    ...
    return "Unknown op $op";

    This can be written much better as

    if (exists($op, %optable)) {
    $optable->$op ($left, $right);
    } else {
    throw_error "Unknown op $op";
    }

    jue
     
    Jürgen Exner, Jan 6, 2006
    #6
  7. Madhu Ramachandran

    robic0 Guest

    On Fri, 06 Jan 2006 04:16:28 GMT, "Jürgen Exner" <> wrote:

    >robic0 wrote:
    >> This is bizzar, why would you use a hash of references to subs to
    >> call a subroutine? Any use for that at all?

    >
    >Oh yes, very common scenario in jump tables.
    >
    >Like in (pseudo-code)
    > if ($op eq "add) then return $left + $right;
    > if ($op eq "minus") then return $left - $right;
    > if ($op eq "div") then return $left / $right;
    > ...
    > return "Unknown op $op";
    >
    >This can be written much better as
    >
    > if (exists($op, %optable)) {
    > $optable->$op ($left, $right);
    > } else {
    > throw_error "Unknown op $op";
    > }
    >
    >jue
    >


    Whats a "jump" table? Whats pesuedo-code?
    $i = 0
    $i = 1-$i
    How far does extrapolation go (down)?
    Is there logic I'm unaware of?
    Can I be fooled?
    Do I have time for un-paid thought?
    Have I done almose everything?
    Can a resume save me?
    -robic0-
     
    robic0, Jan 6, 2006
    #7
  8. Madhu Ramachandran <> wrote:

    > I want to call subroutine, but the name of the subroutine itself is in a
    > variable.



    That is called a "symbolic reference".

    Use a real reference instead:

    perldoc perlreftut

    perldoc perlref



    > sub aSub()

    ^^
    ^^

    Why did you put that there?

    Do you know what it does?

    Is what it does what you want to have done?


    > {
    > my ($arg1) = @_;



    I think not.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 6, 2006
    #8
  9. Gunnar Hjalmarsson, Jan 6, 2006
    #9
  10. Madhu Ramachandran

    robic0 Guest

    On Thu, 5 Jan 2006 22:32:33 -0600, Tad McClellan <> wrote:

    >Madhu Ramachandran <> wrote:
    >
    >> I want to call subroutine, but the name of the subroutine itself is in a
    >> variable.

    >
    >
    >That is called a "symbolic reference".
    >

    Why in hell would you use it in the context of a hash of "subroutines" ?
    Get off your micro-analysis and look at it from a design point of reference.
    Don't ever, ever take any class I teach...

    >Use a real reference instead:
    >
    > perldoc perlreftut
    >
    > perldoc perlref
    >
    >
    >
    >> sub aSub()

    > ^^
    > ^^
    >
    >Why did you put that there?
    >
    >Do you know what it does?
    >
    >Is what it does what you want to have done?
    >
    >
    >> {
    >> my ($arg1) = @_;

    >
    >
    >I think not.
     
    robic0, Jan 6, 2006
    #10
  11. Madhu Ramachandran

    robic0 Guest

    On Thu, 05 Jan 2006 20:28:39 -0800, robic0 wrote:

    >On Fri, 06 Jan 2006 04:16:28 GMT, "Jürgen Exner" <> wrote:
    >
    >>robic0 wrote:
    >>> This is bizzar, why would you use a hash of references to subs to
    >>> call a subroutine? Any use for that at all?

    >>
    >>Oh yes, very common scenario in jump tables.
    >>
    >>Like in (pseudo-code)
    >> if ($op eq "add) then return $left + $right;
    >> if ($op eq "minus") then return $left - $right;
    >> if ($op eq "div") then return $left / $right;
    >> ...
    >> return "Unknown op $op";
    >>

    "jump tables" are an assembly acronymn.
    Do you purport to parse assembly pseudonyms?
    -robic-
     
    robic0, Jan 6, 2006
    #11
  12. Bernard El-Hagin wrote:
    > Gunnar Hjalmarsson wrote:
    >>Tad McClellan wrote:
    >>>Madhu Ramachandran wrote:
    >>>>{
    >>>> my ($arg1) = @_;
    >>>
    >>>I think not.

    >>
    >>Why not?

    >
    > You snipped the bit that answers your question. :)


    Yeah, I see that now. Thanks!

    Tad, would you like a lesson on applying an effective followup style? ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 6, 2006
    #12
  13. robic0 wrote in news::

    >
    > Whats a "jump" table? Whats pesuedo-code?


    Look, if you don't understand anything about elementary computer science,
    that's fine, but you don't have to boast about it.

    --
    Eric
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
     
    Eric J. Roode, Jan 6, 2006
    #13
  14. robic0 wrote in news::

    > Why in hell would you use it in the context of a hash of "subroutines"
    > ? Get off your micro-analysis and look at it from a design point of
    > reference. Don't ever, ever take any class I teach...


    You.... *teach*?!?

    Wow. Just... wow.

    --
    Eric
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
     
    Eric J. Roode, Jan 6, 2006
    #14
  15. Madhu Ramachandran

    Matt Garrish Guest

    "Gunnar Hjalmarsson" <> wrote in message
    news:...
    > Gunnar Hjalmarsson wrote:
    >> Madhu Ramachandran wrote:
    >>> I want to call subroutine, but the name of the subroutine itself is in a
    >>> variable. I was able to do this. However, i also want to pass arguments
    >>> ..

    >>
    >> my $method = 'aSub';
    >> my $aa = 'bla';
    >> my $subref = \&$method;
    >> my $ret = $subref->($aa);
    >> print "ret = $ret\n";
    >>
    >> sub aSub {
    >> my ($arg1) = @_;
    >> print "inside aSub, arg1=$arg1\n";
    >> return 1;
    >> }

    >
    > The above solution 'works', but I'd better admit that it's actually a
    > symref, even if it passes strict. Matt's solution was criticized by that
    > robic0 character, which proves that Matt provided a better answer.
    >


    ROTFL! I'll take the compliment... : )

    Matt
     
    Matt Garrish, Jan 6, 2006
    #15
  16. Thank you very much for the help.
    "Matt Garrish" <> wrote in message
    news:n1jvf.41090$...
    >
    > "Madhu Ramachandran" <> wrote in message
    > news:dpkck1$g8b$...
    >> all:
    >>
    >> I want to call subroutine, but the name of the subroutine itself is in a
    >> variable. I was able to do this. However, i also want to pass arguments
    >> .. not able to do this.

    >
    > You're looking for symbolic references, but it's not good practice to use
    > them. For example:
    >
    > $subRef = 'this_sub';
    > $arg1 = 'first argument';
    > $arg2 = 'second argument';
    >
    > &{$subRef}($arg1, $arg2);
    >
    > sub this_sub {
    > print "$_[0] : $_[1]\n";
    > }
    >
    >
    > It's better practice to use a hash as you won't break the strictures
    > pragma that way, which should make your code easier to maintain:
    >
    > my %subRefs = ( this_sub => \&this_sub );
    >
    > my $subRef = 'this_sub';
    >
    > my $arg1 = 'first argument';
    > my $arg2 = 'second argument';
    >
    > $subRefs{$subRef}($arg1, $arg2);
    >
    > sub this_sub {
    > print "$_[0] : $_[1]\n";
    > }
    >
    > Matt
    >
     
    Madhu Ramachandran, Jan 6, 2006
    #16
  17. Madhu Ramachandran

    Guest

    Madhu Ramachandran <> wrote:
    > I want to call subroutine, but the name of the subroutine itself is in a
    > variable. I was able to do this. However, i also want to pass arguments ..
    > not able to do this.
    > eg: if $sub has the subroutine name, and $arg has the arg to the subroutine.
    > How can i call this from my perl main program? i tried eval() and it worked
    > if i dont have any arguments.


    > #!/usr/local/bin/perl
    >
    > $method="aSub";
    > $aa="\"bla\"";
    > $mm = "$method" . "($aa)";
    > print ("mm = $mm\n");
    > $ret = eval($mm);
    > print ("ret = $ret\n");
    > $ret=eval($method);
    > print ("ret = $ret\n");
    >
    > sub aSub()
    > {
    > my ($arg1) = @_;
    > print ("inside aSub, arg1=$arg1\n");
    > return 1;
    > }


    Try:

    #!/usr/local/bin/perl

    use strict;
    use warnings;

    my $method="aSub";
    my $aa= 'bla';

    no strict;
    my $ret = eval(&$method ($aa));

    use strict;
    print "ret = ", $ret "\n";

    sub aSub() {
    my $arg1 = shift;
    print "inside aSub, arg1=", $arg1, "\n");
    return 1;
    }
     
    , Jan 6, 2006
    #17
  18. On 2006-01-06, Eric J. Roode <> wrote:
    > robic0 wrote in news::
    >
    >> Why in hell would you use it in the context of a hash of "subroutines"
    >> ? Get off your micro-analysis and look at it from a design point of
    >> reference. Don't ever, ever take any class I teach...

    >
    > You.... *teach*?!?
    >
    > Wow. Just... wow.


    In the words of Robert Downey Jr., actually, that clears up a lot of
    things...

    dha

    --
    David H. Adler - <> - http://www.panix.com/~dha/
    Don't you remember High School? I know, for myself, I was a walking
    hormone.
    - Alyson Hannigan
     
    David H. Adler, Jan 6, 2006
    #18
  19. Madhu Ramachandran <> wrote:

    > Thank you very much for the help.



    You are not fooling anyone.

    If you truly were thankful, then you would follow the quoting
    conventions that folks expect here (and nearly everywhere else too).



    DO NOT TOP POST!

    DO NOT FULL-QUOTE!

    Please stop abusing us!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 6, 2006
    #19
  20. Matt Garrish wrote:
    > Gunnar Hjalmarsson wrote:
    >>Gunnar Hjalmarsson wrote:
    >>>


    <code using symref snipped>

    >>The above solution 'works', but I'd better admit that it's actually a
    >>symref, even if it passes strict. Matt's solution was criticized by that
    >>robic0 character, which proves that Matt provided a better answer.

    >
    > ROTFL! I'll take the compliment... : )


    For the sake of completeness, the applicable FAQ entry:

    perldoc -q "variable name"

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 6, 2006
    #20
    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. Bobby Chamness
    Replies:
    2
    Views:
    2,416
    Joe Smith
    Apr 22, 2007
  2. Replies:
    4
    Views:
    428
    Alf P. Steinbach
    May 23, 2007
  3. Replies:
    1
    Views:
    407
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    398
    Victor Bazarov
    May 23, 2007
  5. king
    Replies:
    5
    Views:
    205
Loading...

Share This Page