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

Replies are listed 'Best First'.
Re: lexical sub vs subroutine reference
by dave_the_m (Monsignor) on Jul 26, 2024 at 11:24 UTC
    Lexical subs are little more than syntactic sugar for storing anon subs in a lexical var, but with the calling syntax requiring a few less characters.

    There will not be any significant performance gain.

    Dave.

      I wasn't even aware that lexical subs existed. :-(

      Googling just now found some Effective Perl Programming advice from brian_d_foy (who I respect): Don't use named lexical subroutines

      Is brian's advice still valid today?

      👁️🍾👍🦟
        I think I could have processed the contents of that blog post three times as fast if he had just used normal indenting.

        So I went through all of that, and in the end his conclusion is "it's insanely broken because you cant use them for sort until 5.22 and "our sub" can accidentally redefine methods". So, maybe don't use "our sub", but "my sub" would seem to be perfectly fine. I much prefer to call a sub by name rather than as a function-deref notation from a lexical variable.

        That article uses strong words like "almost irredeemably broken", "incredibly bad idea", which I consider outright wrong.

        The issues with our sub speak { ... } are rather obvious. Technically our makes "lexical" aliases, but I have never seen a use case to use our together with sub.

        A different thing is my sub speak { ... }. This is what I would call a named lexical subroutine, and I use them occasionally. They work for sort since 5.22, which was released nine years ago, so shortly after brian d foy wrote his rant. And they are no longer experimental, they just work.

        While it is correct that you can achieve the same goals with a code reference to an anonymous subroutine, I prefer my sub. It looks "nicer" to me, and I am not distracted by looking whether the code reference is passed to other routines in other packages.