vmraj has asked for the wisdom of the Perl Monks concerning the following question:
Can you please explain how does this line work? $func->(%opts);
This line $func->(%opts) do not have any return statement; But it returns the value properly
ThanksCode: #!/usr/local/bin/perl use strict; use Getopt::Long qw(:config gnu_getopt); ########################################### # Parses the commad line arguments # Example: callfunction.pl -action getInfo ########################################### sub parse_args { my %opts; GetOptions(\%opts, "action=s", ); return %opts; } my %opts = parse_args(); my $action = $opts{action}; my $output = call_function($action, %opts); =head call_function ($function_name, %params) This method invokes the appropriate subroutine based on the funcname p +assed =cut sub call_function { my $funcname = shift; my %opts = @_; my $package = 'main'; if ( my $func = UNIVERSAL::can($package, $funcname) ) { $func->(%opts); } else { die "no such function $package\::$funcname\n"; } } sub getInfo { my $msg = shift; return $msg; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to invoke the method dynamically using universal::can
by ikegami (Patriarch) on Sep 23, 2010 at 15:39 UTC | |
|
Re: How to invoke the method dynamically using universal::can
by Corion (Patriarch) on Sep 23, 2010 at 15:18 UTC |