Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 984391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:00:30+00:00 2026-05-16T05:00:30+00:00

Starting in Perl 5.10, it is now possible to lexically scope the context variable

  • 0

Starting in Perl 5.10, it is now possible to lexically scope the context variable $_, either explicitly as my $_; or in a given / when construct.

Has anyone found good uses of the lexical $_? Does it make any constructs simpler / safer / faster?

What about situations that it makes more complicated? Has the lexical $_ introduced any bugs into your code? (since control structures that write to $_ will use the lexical version if it is in scope, this can change the behavior of the code if it contains any subroutine calls (due to loss of dynamic scope))

In the end, I’d like to construct a list that clarifies when to use $_ as a lexical, as a global, or when it doesn’t matter at all.


NB: as of perl5-5.24 these experimental features are no longer part of perl.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:00:31+00:00Added an answer on May 16, 2026 at 5:00 am

    IMO, one great thing to come out of lexical $_ is the new _ prototype symbol.

    This allows you to specify a subroutine so that it will take one scalar or if none is provided it will grab $_.

    So instead of writing:

    sub foo {
        my $arg = @_ ? shift : $_;
    
        # Do stuff with $_
    }
    

    I can write:

    sub foo(_) {
        my $arg = shift;
    
        # Do stuff with $_ or first arg.
    }
    

    Not a big change, but it’s just that much simpler when I want that behavior. Boilerplate removal is a good thing.

    Of course, this has the knock on effect of changing the prototypes of several builtins (eg chr), which may break some code.

    Overall, I welcome lexical $_. It gives me a tool I can use to limit accidental data munging and bizarre interactions between functions. If I decide to use $_ in the body of a function, by lexicalizing it, I can be sure that whatever code I call, $_ won’t be modified in calling code.

    Dynamic scope is interesting, but for the most part I want lexical scoping. Add to this the complications around $_. I’ve heard dire warnings about the inadvisability of simply doing local $_;–that it is best to use for ( $foo ) { } instead. Lexicalized $_ gives me what I want 99 times out of 100 when I have localized $_ by whatever means. Lexical $_ makes a great convenience and readability feature more robust.

    The bulk of my work has had to work with perl 5.8, so I haven’t had the joy of playing with lexical $_ in larger projects. However, it feels like this will go a long way to make the use of $_ safer, which is a good thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've started using Vim to develop Perl scripts and am starting to find it
Starting with 2005, VS started this behavior of when starting debugging session it spawns
Starting from ASP.NET MVC Preview 3, HTML.Button ( and other related HTML controls) are
Starting a new project and would like to use one of the MVC framworks.
Starting with the error: Error 81 The OutputPath property is not set for this
Starting with the following LINQ query: from a in things where a.Id == b.Id
Starting recently, some of my new web pages (XHTML 1.1) are setup to do
Starting with a already existing installscript 11.5 installer, we created a update installer (differential
Starting to build web applications for mobile devices (any phone). What would be the
Whilst starting to learn lisp, I've come across the term tail-recursive . What does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.