rsennat has asked for the wisdom of the Perl Monks concerning the following question:

hi all

I have an array, which consists of the function names. How do i call that???

use jobs; #local package use args_xml; #local package #the array has function name and its arguments in 2 and 3 @args_input = args_xml::read_file($filename); my $jobs = jobs->instance(); # @jobIds = $jobs->get_jobIds($ARGV[0]); #actual func. call @jobIds = $jobs->$args_input[2]($args_input[3]);

thanks
rsennat

Replies are listed 'Best First'.
Re: calling functions using array variables
by tphyahoo (Vicar) on Nov 28, 2005 at 10:27 UTC
    # first of all, use the strictures: use strict; use warnings; # second of all, pare the question down to its bare essense, # and leave al other extraneous stuff out. my @jobs = ( sub {print "a\n"}, sub {print "b\n"} ); print "one way to call anonymous subs, deferencing with the arrow:\n"; for my $job (@jobs) { $job->(); } # hope this helps! print "another way, deferencing with the & sigil:\n"; for my $job (@jobs) { &$job(); } print "same thing, deferencing with explicit braces:\n"; for my $job (@jobs) { &{$job}(); }
Re: calling functions using array variables
by davorg (Chancellor) on Nov 28, 2005 at 10:38 UTC

    You can call a subroutine whose name is stored in an array element (or, indeed, any scalar variable) like this:

    #!/usr/bin/perl #use strict; use warnings; sub test { print "pass\n"; } my @subs = ('test'); $subs[0]();

    However, note that I had to comment out the "use strict" in my code. This is generally a sign that you are doing something slightly dangerous. In this case you are using a technique called a "symbolic reference" which is really not recommended. The recommended way to do this is to store a reference to the subroutine instead of the subroutine's name. That looks like this:

    #!/usr/bin/perl use strict; use warnings; sub test { print "pass\n"; } my @subs = (\&test); $subs[0]->();

    Notice that in this case we are able to uncomment "use strict".

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: calling functions using array variables
by rsennat (Beadle) on Nov 28, 2005 at 10:38 UTC
    But, i think you did not get my problem here.

    actual function call is like this,
    @jobIds = $jobs->get_jobIds($ARGV[0]);
    which is actually a object reference, from which calling a function with arguments passed.

    I have this function name and arguments in an array now. so the function what i need would be like this, if right.

    @jobIds = $jobs->$args_input[2]($args_input[3]);
    But this is not right. How would I achieve this for a object reference.

    Thanks
    rsennat

      Oh. Ok, so your problem is in calling a method.

      Then the solution is get the name of the method into a scalar variable before trying to call it.

      #!/usr/bin/perl use strict; use warnings; package Foo; sub new { bless {}, shift; } sub test { print "pass\n"; } package main; my @subs = ('test'); my $foo = Foo->new; my $method = $subs[0]; $foo->$method;
      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        my $method = $subs[0]; $foo->$method;

        Or, if you're in the mood for obfuscation:

        $foo->${ \$subs[0] };
        Thats really gr8 stuff!!!!!!!!!!!!!!

        For calling methods from array variables. cool.

        Thanks a lot.

        rsennat