develooper Front page | perl.perl5.porters | Postings from September 2003

Re: [perl #24069] Strange ++substr behaviour

Thread Previous
From:
Yitzchak Scott-Thoennes
Date:
September 30, 2003 00:59
Subject:
Re: [perl #24069] Strange ++substr behaviour
Message ID:
20030930075839.GA1980@efn.org
On Mon, Sep 29, 2003 at 11:45:24PM -0000, "book@mongueurs.net (via RT)" <perlbug-followup@perl.org> wrote:
>     my $c = "bzr";
>     print ++substr($c, 1, 1), " $c";
> 
> ouputs
> 
>     a baar
> 
> when I think it should really output
> 
>     aa baar

Basically, the substr is used first as the operand of ++, replacing
the z with aa.  Then ++ returns the lvalue substr to print.  At that
point it is still only providing a one character wide "window" of $c.

The following disclaimer was added to the 5.8.1 perlfunc.pod:

> If the lvalue returned by substr is used after the EXPR is changed in
> any way, the behaviour may not be as expected and is subject to change.
> This caveat includes code such as C<print(substr($foo,$a,$b)=$bar)> or
> C<(substr($foo,$a,$b)=$bar)=$fud> (where $foo is changed via the
> substring assignment, and then the substr is used again), or where a
> substr() is aliased via a C<foreach> loop or passed as a parameter or
> a reference to it is taken and then the alias, parameter, or deref'd
> reference either is used after the original EXPR has been changed or
> is assigned to and then used a second time.

in the hopes of perhaps cleaning this up a bit for 5.10.0.  I'd like
at that point for substr to change so that the "window" resizes when
assigned to, which should get you your "aa baar".  The more
complicated part is what to do when the offset or length given is
negative.  I haven't figured that out yet.

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