Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hash of functions in modules

Reply
Thread Tools

Hash of functions in modules

 
 
Vito Corleone
Guest
Posts: n/a
 
      11-30-2004
Hi,

I have code like this:

if ($cmd eq "get") { $self->get(); }
elsif ($cmd eq "set") { $self->set(); }
elsif ($cmd eq "copy") { $self->copy(); }

I want to put it inside hash of function. How do I do that? I know I can
do:

%hof = ( get => \&get,
set => \&set,
copy => \&copy );
$hof{$cmd}->();

But I will lose the $self in the function. How do I pass $self to the
function too as well?
 
Reply With Quote
 
 
 
 
Ala Qumsieh
Guest
Posts: n/a
 
      11-30-2004
Vito Corleone wrote:

> if ($cmd eq "get") { $self->get(); }
> elsif ($cmd eq "set") { $self->set(); }
> elsif ($cmd eq "copy") { $self->copy(); }


Perl allows this:

$self->$cmd;

--Ala

 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      11-30-2004
Also sprach Ala Qumsieh:

> Vito Corleone wrote:
>
>> if ($cmd eq "get") { $self->get(); }
>> elsif ($cmd eq "set") { $self->set(); }
>> elsif ($cmd eq "copy") { $self->copy(); }

>
> Perl allows this:
>
> $self->$cmd;


And in case $cmd can contain garbage (that is, the name of a
non-existing method), one could catch that with an autoloader:

our $AUTOLOAD;
...

sub DESTROY { }
sub AUTOLOAD {
die "No such method: $AUTOLOAD";
}

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
imputils - import problem modules from sys.modules Remy Cool Python 1 08-27-2003 02:25 PM



Advertisments