Let's assume I have the following action in a Catalyst::Controller that requires the parameter bar to be present. If this parameter is not given in the query I want to show an error message:
sub foo : Local {
my ($self, $c) = @_;
if (!$c->req->params->{bar}) {
$c->stash->{error} = "Parameter 'bar' missing!";
$c->detach; # or return; ?
}
# some more logic...
}
Now my question is: does it make a difference whether I do $c->detach or simply return from the action? At first glance the behaviour seems to be identical, but are there any advantages or disadvantages of either option?
According to the documentation for
detach():Inspecting the source, see line 278, we see that
detach()throws aCatalyst::Exception::Detachexception and thus never returns to the caller.So using
$c->detach()is not the same as callingreturnto return from the action sub. Callingreturnwill continue the action processing chain, while$c->detach()will not.