develooper Front page | perl.perl5.porters | Postings from April 2006

Re: [PATCH] t/op/pat.t using test.pl

Thread Previous | Thread Next
From:
Ronald J Kimball
Date:
April 27, 2006 07:52
Subject:
Re: [PATCH] t/op/pat.t using test.pl
Message ID:
20060427145220.GA2390@penkwe.pair.com
On Thu, Apr 27, 2006 at 04:06:35PM +0200, David Landgren wrote:
> Jim Cromie wrote:
> >does it matter that $1 is only set on a match (IIRC), and that an old value
> >could accidentally match the expected  ?  ('foo' being as common as it is)
> >I imagine the tests avoided that potential..
> 
> You are quite right, they do, so some defensive coding would be required.
> 
> perl -le '$_="ab";/(a)(b)/; print "X$1$2"; /(c)/; print "Y$1$2"'
> Xab
> Yab
> 
> perl -le '$_="ab";/(a)(b)/; print "X$1$2"; undef=~/()/; /(c)/; print 
> "Y$1$2"'
> Xab
> Y
> 
> Or through local
> 
> perl -le '$_="ab";{local $1,$2;/(a)(b)/; print "X$1$2"} {local $1,$2; 
> /(c)/; print "Y$1$2"}'
> Xab
> Y

You don't need local; the curly braces are sufficient.

perl -le '{$_="ab";/(a)(b)/; print "X$1$2";} {/(c)/; print "Y$1$2"}'
Xab
Y


Ronald

Thread Previous | Thread Next


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