[Catalyst] Re: Private Chained Actions?

Jonathan Rockway jon at jrock.us
Fri Jun 20 21:12:47 BST 2008


* On Fri, Jun 20 2008, Marc Sebastian Pelzer wrote:
>>> You need a screwdriver, not a hammer.
>>
>>    sub hello_setup : Private {
>>      my ($self, $c) = @_
>>      $c->stash->{setup} = 'done';
>>    }
>>
>>    sub hello : Private {
>>      my ($self, $c) = @_
>>      $c->forward('hello_setup'); # <---------
>>      $c->res->body('hello! setup is ' . $c->stash->{setup});
>>    }
>>
>>    sub index : Path : Args(0) {
>>      my ($self, $c) = @_
>>      $c->detach('hello');
>>    }
>
>
> thanks for the hint. Thats what I'm doing right now. I thought that
> there may be a more elegant way of doing this. Especially for a
> longer/ more complex chained setup :) Also, doing it this way it does
> not look  not too nice in the dispatcher path in the debug logfile -
> hello()  comes before hello_setup() :-|

Yeah, that's ugly.  What you really want are method modifiers:

    package MyApp::Controller::Foo;
    use Moose;
    BEGIN { extends 'Catalyst::Controller' };
    
    sub main_page :Path Args(0) {
        my ($self, $c) = @_;
        $self->hello($c);
    }
    
    sub hello {
        my ($self, $c) = @_;
        $c->res->body('hello! setup is'. $c->stash->{setup});
    }
    
    before hello => sub {
        my ($self, $c) = @_;
        $c->stash->{setup} = 'done';    
    };
    
    1;

You can also say:

   before [qw/method1 method2 method3 .../] => sub { ... }

to do something before a bunch of methods at once.  See "perldoc Moose"
for details.

Regards,
Jonathan Rockway

-- 
print just => another => perl => hacker => if $,=$"



More information about the Catalyst mailing list