Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: lexical sub vs subroutine reference

by dave_the_m (Monsignor)
on Jul 26, 2024 at 11:24 UTC ( [id://11160776]=note: print w/replies, xml ) Need Help??


in reply to lexical sub vs subroutine reference

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.

  • Comment on Re: lexical sub vs subroutine reference

Replies are listed 'Best First'.
Re^2: lexical sub vs subroutine reference
by eyepopslikeamosquito (Archbishop) on Jul 26, 2024 at 12:16 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11160776]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-09-09 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.