http://www.perlmonks.org?node_id=11160775

tomred has asked for the wisdom of the Perl Monks concerning the following question:

I have a small piece of code ($error_sub) that I need to use in two places within the same subroutine. It looks some like this (a Catalyst App).
sub myaction : Chained('.') PathPart('myaction') POST { my ( $self, $c ) = @_; my $form = MyApp::Form->new; $form->process(params => $c->req->body_params); my $error_sub = sub { $c->response->status(400); $c->stash( results => { form => $form->render, }, ); $c->detach(); }; if ( $form->is_valid ) { try { $form->update_model; } catch($e) { $form->add_form_error($e); $error_sub->(); }; } else { $error_sub->(); } }
Scope is not an issue in this context as far as I can tell.
My question is whether there are benefits to using lexical subs in this situation. I would end up with
my sub error { $c->response->status(400); $c->stash( results => { form => $form->render, }, ); $c->detach(); }
Would I be save a CPU cycle or a byte of RAM using lexical subs, or is there any other benefits? Thanks in advance, Dermot