develooper Front page | perl.perl5.porters | Postings from February 2011

Re: Bug with given() localization, pos() magic, and m//gc state

Thread Previous | Thread Next
Eric Brine
February 20, 2011 19:23
Re: Bug with given() localization, pos() magic, and m//gc state
Message ID:
On Sun, Feb 20, 2011 at 6:45 PM, Father Chrysostomos <>wrote:

> Tom Christiansen wrote:
> > (Tested on 5.13.9.)
> >
> > I can't figure out whether I've found a bug, or whether I'm being dense.
> > What happens is that if you do not manually reset the pos state when you
> > finally hit the end of the string, the next time through you are still at
> > the end.  Well, of course.  Right.
> >
> > But this happens when it is not the same variable anymore!  That is, it's
> > a new my variable that we're localizing via given().  It seems that the
> > pos() magic isn't getting reset when I feel that it should be.  Only when
> > you manually reset it do things get better, and no combination of my()
> > and local() makes any difference.
> It *is* the same variable. given() re-uses the same lexical $_ (its own)
> each time.
> given() doesn’t do any localisation or aliasing. ‘given($foo) { ++$_ }’ is
> a no-op.

So how come it doesn't act like an assignment when it comes to pos?

>perl -E"$_ = 'abc'; pos = 1; say pos//'u'; $_ = def; say pos//'u';"

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About