Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Passing arguments to functions.

by balajinagaraju (Sexton)
on Apr 25, 2012 at 10:43 UTC ( [id://967037]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks. I am trying to call a function by passing a scalar variable and a array reference as a argument. The function call is as below.

&executeCommand($command, \@paramlist);
Here the parameter $command is again a name of a function module which needs to be triggered with the \@paramlist as the argument list for the function pointed by $command. In the function definition of executeCommand i should call the function pointed by the $command with the \@paramlist as the arguments for that function. How do i do that , i am able loop through the parameter list and get the values but how can i trigger the function.Kindly suggest how to proceed. Definition of executeCommand is as below

sub executeCommand{ print "Inside ExecuteCommand\n"; print "Currently executing Command name:".$_[0]; print "\n"; my $array_reference = $_[1]; foreach my $element (@$array_reference){ print "Parameter:".$element."\n"; } }

Replies are listed 'Best First'.
Re: Passing arguments to functions.
by BrowserUk (Patriarch) on Apr 25, 2012 at 11:26 UTC

    #! perl -slw use strict; { package Other; sub commandFunc { print "Doing Other::commandFunc with args: [ @{ $_[0] } ]"; return; } } sub executeCommand{ no strict 'refs'; my( $func, $argRef ) = @_; *{$func}->( $argRef ); } my @a = 1.. 10; executeCommand( 'Other::commandFunc', \@a ); __END__ C:\test>junk Doing Other::commandFunc with args: [ 1 2 3 4 5 6 7 8 9 10 ]

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      The use of symbolic reference is an elegent solution for a 'use once' script. The effort to write a hash of hard references (Called 'dispatch table' in a previous post) will probably be repaid in the maintainence of 'industrial strength' software. Unfortunately, most perl applications lie somewhere inbetween, where the choice is one of preference.
        The effort to write a hash of hard references (Called 'dispatch table' in a previous post) will probably be repaid in the maintainence of 'industrial strength' software.

        Sorry, but you do not know what you are talking about.

        The code I posted uses a hash-based dispatch table. It is called the symbol table.

        There is simply no advantage to adding another layer of indirection here; and no additional risk or maintenance effort in avoiding that extra layer.

        Which makes all your talk of "use once" and "industrial strength", nothing more than cargo-culted, pseudo-CompSci balderdash!


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

        You don't need symbolic references at all

        #!/usr/bin/perl -- use strict; use warnings; use CGI; sub dier { die " dier die @_ " } warn main->can('CGI::new')->(); main->can('dier')->( 'now' ); __END__ CGI=HASH(0x99a1fc) at - line 5. dier die now at - line 4.
Re: Passing arguments to functions.
by Anonymous Monk on Apr 25, 2012 at 11:05 UTC
Re: Passing arguments to functions.
by Anonymous Monk on Apr 25, 2012 at 11:17 UTC
    What is a "function module"? An example of what $command actually contains would help.

    I recommend passing a code reference as first argument, this is the most generic way to solve this kind of problem.

    use 5.010; use strictures; sub execute_command { my ($code, $params) = @_; say 'Inside executeCommand'; say 'Currently executing: ', ref $code; foreach my $element (@{ $params }) { say "Parameter: $element"; $code->($element); } } my $command = sub { # I am going to print the arguments I receive! say @_; }; execute_command($command, \@paramlist);
      Hi, Thanks for your reply, i will try your approach and let you know the results. $command is a normal perl function , it might do anything as you defined it can just print the passed arguments as well. In my case $command is a value got by parsing an xml which is a name of a function.. $command = launchapp; launchapp is the function name which needs to be triggered.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://967037]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found