develooper Front page | perl.perl5.porters | Postings from December 1999

Re: [ID 19991128.001] The value of pattern match vars. after `next'

Thread Previous
From:
Inaba Hiroto
Date:
December 3, 1999 06:34
Subject:
Re: [ID 19991128.001] The value of pattern match vars. after `next'
Message ID:
3847D418.30B673EB@st.rim.or.jp
Ilya Zakharevich wrote:
> 
> Inaba Hiroto writes:
> > After `next' or `redo', pattern match variables are reset to the block's
> > initial value.
> >
> >   $_ = foo; /foo/;
> >   for (qw/bar baz/) { print "$&\n" unless /bar/; }      # print "bar\n"
> >   for (qw/bar baz/) { next if /bar/;  print "$&\n"; }   # print "foo\n"
> >
> > I think it is a bug
> 
> What made you think so?  What other block-local things behave like this?

I think $& etc. are block local like "{ local $var = $var; ... }".

  $x = "foo";
  for (qw/bar baz/) { local $x=$x; print "$x\n" unless /bar/ and $x="bar"; }
  for (qw/bar baz/) { local $x=$x; next if /bar/ and $x="bar"; print "$x\n"; }

This code print "foo\nfoo\n". If the former code print "foo\nfoo\n", I think
it is consistent except the description of $& in "perlvar.pod".

       $&      The string matched by the last successful pattern
               match (not counting any matches hidden within a
               BLOCK or eval() enclosed by the current BLOCK).

So I think the former code should print "bar\nbar\n" and current behavior
is a bug.
--
			Inaba Hiroto <inaba@st.rim.or.jp>


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