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

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

Thread Previous
Yitzchak Scott-Thoennes
September 30, 2003 00:59
Re: [perl #24069] Strange ++substr behaviour
Message ID:
On Mon, Sep 29, 2003 at 11:45:24PM -0000, " (via RT)" <> 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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About