Hash of functions in modules

Discussion in 'Perl Misc' started by Vito Corleone, Nov 30, 2004.

  1. 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?
     
    Vito Corleone, Nov 30, 2004
    #1
    1. Advertising

  2. Vito Corleone

    Ala Qumsieh Guest

    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
     
    Ala Qumsieh, Nov 30, 2004
    #2
    1. Advertising

  3. 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$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Nov 30, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,609
    Victor Bazarov
    Jun 21, 2004
  2. Remy Cool
    Replies:
    1
    Views:
    432
    Remy Cool
    Aug 27, 2003
  3. Tobiah
    Replies:
    2
    Views:
    312
    Tobiah
    Sep 14, 2003
  4. rp
    Replies:
    1
    Views:
    537
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    625
    David A. Black
    Jul 2, 2008
Loading...

Share This Page