i trying yo doing the the following pseudocode in perl
#!/usr/bin/perl -w
#App.pm
use strict;
use OtherModule;
use Other2Module;
sub App::hashF
{
my $hash_funtion = {
'login' => OtherModule::login,
'logout' => Other2Module::logout
};
my($module, $params) = @_;
return $hash->{$module}($params);
}
but i get error like:
– Can’t use string (“login”) as a subroutine ref while “strict refs”
– Can’t use bareword (“OtherModelo”) as a HASH ref while “strict refs”
I decided to enhance your code:
We cannot assign bare names, but we can pass around code references. The
&Sigil denotes the “code” type or a subroutine, and\gives us a reference to it. (Not getting a reference would execute the code; not something we want. Never execute&subroutineunprovoked.)BTW: Hashes can only hold scalar values, and (code) references are a kind of scalar.
When we want to call our sub from the hash, we have to use the dereference operator
->.$hash->{$module}returns a code reference as value;->(@arglist)executes it with the given arguments.Another BTW: Don’t write
App::hashFunless you are working inside an external module. You can declare your current namespace by writingpackage Appor whatever name you like (should correspond with path/name of .pm file).