On Saturday 31 Oct 2009 02:38:11 Steve Bertrand wrote:
> Steve Bertrand wrote:
> > Hi all,
> >
> > Within a CGI environment, I'm trying to do a dispatch table test, but
> > can't figure out how to call the coderef as a method. Here is the
> > working code. I'll describe what doesn't work afterwards:
> >
> > sub perform_find {
> >
> >     my $self    = shift;
> >
> >     my $find_this   = $self->query->param( 'find_this' );
> >     my $search_data = $self->query->param( 'search_data' );
> >
> >     my %find_commands = (
> >
> > # call by local function
> >                 plan_by_id  => \&_find_plan_by_id,
> >             );
> >
> > # and pass in $self explicitly
> >     $find_commands{ $find_this }( $self, $search_data );
> > }
> >
> > __END__
> >
> > Things progress properly using the above. What I don't understand, is
> > why I have to pass in $self as a param to the dispatch table. What
> > didn't work is this:
> >
> >
> > sub perform_find {
> >
> >     my $self    = shift;
> >
> >     my $find_this   = $self->query->param( 'find_this' );
> >     my $search_data = $self->query->param( 'search_data' );
> >
> >     my %find_commands = (
> >
> > # I'm trying to call the method on myself
> >                 plan_by_id  => \&{ $self->_find_plan_by_id },
> >             );
> >

This should probably be: (untested)

<<<<
        plan_by_id => sub { return $self->_find_plan_by_id(@_); },
>>>>

See:

* http://perl-begin.org/tutorials/perl-for-newbies/part3/#page--
refs_to_funcs--DIR

(Sorry for the broken URL)

* http://www.shlomifish.org/lecture/Perl/Newbies/lecture3/refs_to_funcs/

(Same resource - only as multiple pages).

> > # ...obj passed in implicitly as expected
> >     $find_commands{ $find_this }( $search_data );

This should be:

{{{
$find_commands{$find_this}->($search_data)
}}}

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://shlom.in/hhfg

Chuck Norris read the entire English Wikipedia in 24 hours. Twice.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to