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

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

From:
Norbert Buchmuller via RT
Date:
October 24, 2008 10:12
Subject:
[perl #58072] Feature request: regexp flag to not set $1, $2 etc.
Message ID:
rt-3.6.HEAD-29762-1224754811-397.58072-15-0@perl.org
On Tue Aug 19 00:34:49 2008, abigail@abigail.be wrote:
> 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
> 

Yeah, that's exactly the method I chose for fixing NEXT
(http://rt.cpan.org/Ticket/Display.html?id=36956). Unfortunately nobody
had the time to review and apply my patch. :-(



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