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

  • SEARCH
  • Home
  • 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 257133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:04:18+00:00 2026-05-11T22:04:18+00:00

What is the scope of $1 through $9 in Perl? For instance, in this

  • 0

What is the scope of $1 through $9 in Perl? For instance, in this code:

sub bla {
    my $x = shift;
    $x =~ s/(\d*)/$1 $1/;
    return $x;    
}

my $y;

# some code that manipulates $y

$y =~ /(\w*)\s+(\w*)/;

my $z = &bla($2);
my $w = $1;

print "$1 $2\n";

What will $1 be? Will it be the first \w* from $x or the first \d* from the second \w* in $x?

  • 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-11T22:04:18+00:00Added an answer on May 11, 2026 at 10:04 pm

    from perldoc perlre

    The numbered match variables ($1, $2, $3, etc.) and the related punctuation set ($+ , $& , $` , $’ , and $^N ) are all dynamically scoped until the end of the enclosing block or until the next successful match, whichever comes first. (See “”Compound Statements”” in perlsyn.)

    This means that the first time you run a regex or substitution in a scope a new localized copy is created. The original value is restored (à la local) when the scope ends. So, $1 will be 10 up until the regex is run, 20 after the regex, and 10 again when the subroutine is finished.

    But I don’t use regex variables outside of substitutions. I find much clearer to say things like

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    sub bla {
        my $x = shift;
        $x =~ s/(\d*)/$1 $1/;
        return $x;    
    }
    
    my $y = "10 20";
    
    my ($first, $second) = $y =~ /(\w*)\s+(\w*)/;
    
    my $z = &bla($second);
    my $w = $first;
    
    print "$first $second\n";
    

    where $first and $second have better names that describe their contents.

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

Sidebar

Related Questions

The scope of this is that we have three main projects. Some of the
I am working my way through some Objective-C code that I did not write
Following code iterates through many data-rows, calcs some score per row and then sorts
I need to write a scope that I will pass a user's id through
Can I get the scope of process code in the memory through PE file
Im looking through some code (unfortunatly the author isnt around anymore) and im wondering
This is the nth question about JS variable scope but I read through the
I need some clearance on scope of instance variables in rails application (not sure
Global scope allows you to use a variable in a function that was defined
i am stuck at scope resolution in python. let me explain a code first:

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.