Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: how to declare a local *subname=sub{}?

by Corion (Patriarch)
on Oct 31, 2016 at 09:48 UTC ( [id://1175004]=note: print w/replies, xml ) Need Help??


in reply to how to declare a local *subname=sub{}?

As you haven't told us what problems you're trying to address it's hard to give you a good direction to investigate in.

There is an experimental feature named lexical_subs that is supposed to declare lexically scoped subroutine names in the following form:

use feature 'lexical_subs'; sub foo { my sub secret_foo { my($arg) = @_; return "Hello $arg"; }; my( $bar ) = @_; my $res = secret_foo( $bar ); return "Result is '$res'"; } print foo('World');

But as far as I know, the feature never left its experimental status and has the same interactions with lexical variables as the traditional approach using lexical variables has. If you want to use local subroutines without polluting the namespace, just use anonymous subroutines:

use feature 'lexical_subs'; sub foo { my $secret_foo = sub { my($arg) = @_; return "Hello $arg"; }; my( $bar ) = @_; my $res = $secret_foo->( $bar ); return "Result is '$res'"; } print foo('World');

Using dynamic scope is usually fraught with problems as debugging dynamic scope is difficult. There is a reason why lexical scope was a great thing for Perl 5, and while it is sad that there is no convenient lexical scope for subroutines, working around this with dynamic scope is a bad idea.

Replies are listed 'Best First'.
Re^2: how to declare a local *subname=sub{}? [non-experimental lexical_subs]
by kcott (Archbishop) on Nov 04, 2016 at 07:08 UTC
    "There is an experimental feature named lexical_subs ... But as far as I know, the feature never left its experimental status ..."

    This will probably be non-experimental in the next stable release (i.e. v5.26). See "perl5252delta: Lexical subroutines are no longer experimental".

    Update: Added "[non-experimental lexical_subs]" to title.

    — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-19 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found