Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: lexical sub vs subroutine reference

by eyepopslikeamosquito (Archbishop)
on Jul 26, 2024 at 12:16 UTC ( [id://11160777]=note: print w/replies, xml ) Need Help??


in reply to Re: lexical sub vs subroutine reference
in thread lexical sub vs subroutine reference

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?

👁️🍾👍🦟
  • Comment on Re^2: lexical sub vs subroutine reference

Replies are listed 'Best First'.
Re^3: lexical sub vs subroutine reference
by NERDVANA (Curate) on Jul 26, 2024 at 14:21 UTC
    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.

Re^3: lexical sub vs subroutine reference
by haj (Vicar) on Jul 26, 2024 at 19:37 UTC

    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://11160777]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-09-08 10:16 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.