Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

call sub_function easily

by dideod.yang (Sexton)
on Mar 03, 2019 at 08:25 UTC ( [id://1230776]=perlquestion: print w/replies, xml ) Need Help??

dideod.yang has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks. I have question about method of call sub_function easily. I generate array of sub_function. When I call variable in array, then I expect to execute sub_function. but It is't working.. Do you have easy way to call sub_function. I don't want to write all of sub_function to work.
use strict; use warnings; ## examples of sub_function. sub test { print "test\n" } sub test2 {print "test2\n" } sub test3 {print "test3\n" } ## pair up variables with sub_function. my $test = \&test; my $test2 = \&test2; my $test3 = \&test3; my @array = qw/ test test2 test3 /; ## I want to call sub_function each $array. but it is not working.. foreach my $array (@array) { &$$array } ## I want to print test test2 test3 like below examples. ## But below examples is not that good way. please help me :) foreach my $array (@array) { if($array eq "test" ) { $test } if($array eq "test2") { $test2} if($array eq "test3") { $test3} }

Replies are listed 'Best First'.
Re: call sub_function easily
by Athanasius (Archbishop) on Mar 03, 2019 at 09:33 UTC
Re: call sub_function easily
by tybalt89 (Monsignor) on Mar 03, 2019 at 09:34 UTC
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1230776 use strict; use warnings; ## examples of sub_function. sub test { print "test\n" } sub test2 {print "test2\n" } sub test3 {print "test3\n" } ## pair up variables with sub_function. my $test = \&test; my $test2 = \&test2; my $test3 = \&test3; #my @array = qw/ test test2 test3 /; my @array = ( $test, $test2, $test3 ); foreach my $array (@array) { $array->() }
Re: call sub_function easily (updated)
by haukex (Archbishop) on Mar 03, 2019 at 08:42 UTC

    <update> Sorry, I wasn't quite awake yet, my reply below isn't as relevant to the question as I thought it was... Athanasius is right: using a hash table is IMO best (don't use eval or symbolic references). And tybalt89 showed how to make an array of coderefs, which is another good option, depending on your requirements (it doesn't allow you to look up stuff by name). </update>

    To deference a code reference, one would normally use $coderef->(). It's also possible to say &$coderef() or &$coderef, but the first form is more modern. See perlreftut, perlref, and perlsub.

    I'm not sure what you mean by the second part of your question, whether you want to call the subs, or get their original name? If it's the latter:

    use Sub::Util qw/subname/; sub foo {} my $test = \&foo; print subname($test); # prints "main::foo"

    Sub::Util has been in the core since Perl v5.22.

Re: call sub_function easily
by tybalt89 (Monsignor) on Mar 03, 2019 at 17:31 UTC

    And now for something completely different :)

    #!/usr/bin/perl # https://perlmonks.org/?node_id=1230776 use strict; use warnings; ## pair up variables with sub_function. my @array = qw/ test test2 test3 /; my %easyily; for my $array ( @array ) { $easyily{$array} = sub { print "$array\n" }; } for my $array ( @array ) { $easyily{$array}() }
Re: call sub_function easily
by karlgoethebier (Abbot) on Mar 03, 2019 at 10:08 UTC

    Not sure what you are looking for. May be it is something like my $testomato = sub {shift};. You call it as say $testomato->(q(test));. Regards, Karl.

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypts_hex($ENV{KARL});'Help

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found