[Catalyst] overriding the match method for custom actions

John Napiorkowski jjn1056 at yahoo.com
Thu Feb 8 18:03:32 GMT 2007


--- John Napiorkowski <jjn1056 at yahoo.com> wrote:

> Hi,
> 
> Does anyone know the correct way to override the
> match
> method of an Action?  The docs give a great example
> for overriding the execute method, and there are
> some
> CPAN examples showing how to override dispatch, but
> I
> can't seem to find one for match.  From looking at
> the
> source code I gather that this function should
> return
> 1 if the match is successful and false otherwise.  I
> have two questions:
> 
> 1) When the match fails should I "return;" or do I
> need to literally "return 0;".  Both seem to work,
> but
> I haven't done too many tests.  Does anyone know?
> 
> 2) It's correct to explicitly return the result of
> the
> superclass?  Something like:
> 
> sub match
> {
>   my ( $self, $c ) = @_;
> 
>   ## where _check_custom_match does something and
> then
>   ## return 1 or 0 based on the outcome.
> 
>   my $status = $self->_check_custom_match($c) &&
>    $self->NEXT::match( @_ );
> 
>   return $status;
> }
> 
> Does this look right to you?
> 
> BTW, does anyone know if your custom matches get run
> when doing a $c->uri_for($c->action, [@Args])?  What
> I
> mean is that $c->uri_for($c->action, ...) will
> return
> nothing if you don't pass the expected arguments,
> which tells me that the match method must get
> invoked,
> but I'm not sure if my custom matches also get
> invoked.  I think it would be great if it did. 
> Anyway, I'll experiment a bit and try to figure it
> out.
> 
> Thanks!
> John

I did some investigating for the $c->uri_for question.
 It seems that you can indeed use this to create URI
objects that won't pass the match test.  For example:

sub test2 :Local :Args(1)
{
  my ($self, $c) = @_;

  $c->log->_dump( $c->uri_for($c->action, 10000) );
  $c->log->_dump( $c->uri_for($c->action) );	
}

Both dumps give a URL object, although the second one
returns a link that won't work, since the 'test2'
action requires an argument.  This is seems a bit
different behavior from when you try to create a uri
from an action that is the endpoint in a chain; in
that case if you don't pass the correct number of args
it will return undef.  However even chained actions
don't run the match methods, so I guess it's really
the same. 

Changing this would be expensive I think.  Sounds like
something that would make a good plugin though.  Just
something to remember.

For the overriding match method issue for custom
Action Classes I settled on a standard pattern which
seems to work properly.  Looks like:

sub match
{
  my ( $self, $c ) = (shift, @_);

  ## where _validate_args does something and then
  ## return 1 or 0 based on the outcome.

  my $check   = $self->_validate_args($c);
  my $status  = $check ? $self->NEXT::match(@_) : 0;

  return $status;
}

> 
>  
>
____________________________________________________________________________________
> No need to miss a message. Get email on-the-go 
> with Yahoo! Mail for Mobile. Get started.
> http://mobile.yahoo.com/mail 
> 
> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo:
> http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive:
>
http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
> 



 
____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com



More information about the Catalyst mailing list