develooper Front page | perl.perl5.porters | Postings from August 2008

Re: [perl #58072] Feature request: regexp flag to not set $1, $2etc.

Thread Previous
From:
Abigail
Date:
August 19, 2008 00:34
Subject:
Re: [perl #58072] Feature request: regexp flag to not set $1, $2etc.
Message ID:
20080819073433.GB4145@almanda
On Mon, Aug 18, 2008 at 09:00:06AM -0700, Ed Avis wrote:
> # New Ticket Created by  "Ed Avis" 
> # Please include the string:  [perl #58072]
> # in the subject line of all future correspondence about this issue. 
> # <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=58072 >
> 
> 
> This is a bug report for perl from eda@waniasset.com,
> generated with the help of perlbug 1.36 running under perl 5.10.0.
> 
> 
> -----------------------------------------------------------------
> [Please enter your report here]
> 
> When a regexp has capturing parentheses it sets match variables from
> $1 upwards.  This can sometimes cause trouble, for example
> 
> <http://rt.cpan.org/Ticket/Display.html?id=36956>
> <http://rt.perl.org/rt3/Public/Bug/Display.html?id=23140>
> <http://rt.perl.org/rt3/Public/Bug/Display.html?id=22369>
> 
> In most situations you can avoid the problem by following two rules:
> do not pass $1 as an argument to subroutines, and any subroutine
> should unpack @_ first before doing any regexp operations.  But this
> is not always possible, as in the first bug report above for NEXT.
> 
> It would simplify these situations to have a flag meaning do not
> change the magical match variables.  Perhaps it could be called /l but
> the exact letter is not important at all.  Then
> 
>     my $str = 'hello';
>     my ($x, $y) = ($str =~ /(\w)(\w)/l);
> 
> would set $x to 'h' and $y to 'e' as now, but would not touch the old
> values of $1 and $2.
> 
> In module code where you have to be cautious about trampling on other
> people's $1 and $2, the /l flag would let you use regular expressions
> without a lot of shenanigans.


You can already do this easily currently:

    "foo" =~ /(\w)(\w)/;
    say "$1 $2";
    my $str = 'hello';
    my ($x, $y) = do {$str =~ /(\w)(\w)/};
    say "$x $y $1 $2";
    __END__
    f o
    h e f o


Abigail

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About