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

Re: bug? $& temporarily modified in look-ahead

Thread Previous | Thread Next
From:
Hugo van der Sanden
Date:
August 15, 2001 04:40
Subject:
Re: bug? $& temporarily modified in look-ahead
Message ID:
200108151042.f7FAg5L31052@crypt.compulink.co.uk
"Jeff 'japhy/Marillion' Pinyan" <jeffp@crusoe.net> wrote:
:This is forwarded from a conversation I had with Jeff Friedl.
:
:==========================================================================
:Jeff 'japhy/Marillion' Pinyan <jeffp@crusoe.net> wrote:
:
:|> The only problem is (understandable) that $&, while updated in a
:|> look-ahead, is NOT "down"dated in a look-behind:
:|> 
:|>   "japhy" =~ m{
:|>     ..
:|>     (?{ print "<$`><$&><$'>" })
:|>     (?=
:|>       ..
:|>       (?{ print "<$`><$&><$'>" })
:|>     )
:|>     (?<=
:|>       ..
:|>       (?{ print "<$`><$&><$'>" })
:|>     )
:|>     (?{ print "<$`><$&><$'>" })
:|>   }x;
:|> 
:|> The output is:
:|> 
:|>   <><ja><phy>
:|>   <><japh><y>
:|>   <><ja><phy>
:|>   <><ja><phy>
:
:The 2nd one looks like a bug to me. The lookahead is just that, lookahead,
:and so I feel that it should never (ever) be part of $&. The other ones
:seem as I would expect them.

If you change the lookbehind to this:
  (?<= 
    (?{ print "<$`><$&><$'>" })
    ..
  )    
you get:
  <><ja><phy>
  <><japh><y>
  <><><japhy>
  <><ja><phy>

.. which makes slightly more sense.

We don't currently sign any contract to guarantee the value of $& and
friends inside a regexp. I'm not convinced that we ought to - if you
choose to document this, I'd strongly recommend a warning that it may
change in the future.

Hugo

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