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).
My question is whether there are benefits to using lexical subs in this situation. I would end up with
Scope is not an issue in this context as far as I can tell.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->(); } }
My question is whether there are benefits to using lexical subs in this situation. I would end up with
Would I be save a CPU cycle or a byte of RAM using lexical subs, or is there any other benefits? Thanks in advance, Dermotmy sub error { $c->response->status(400); $c->stash( results => { form => $form->render, }, ); $c->detach(); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: lexical sub vs subroutine reference
by dave_the_m (Monsignor) on Jul 26, 2024 at 11:24 UTC | |
by eyepopslikeamosquito (Archbishop) on Jul 26, 2024 at 12:16 UTC | |
by NERDVANA (Curate) on Jul 26, 2024 at 14:21 UTC | |
by haj (Vicar) on Jul 26, 2024 at 19:37 UTC |
Back to
Seekers of Perl Wisdom