Re: [perl #56986] 5.8.8: bug in substr() as lvalue?
by Yitzchak Scott-Thoennes other posts by this author
Jul 16 2008 11:45AM messages near this date
Re: [perl #56986] 5.8.8: bug in substr() as lvalue?
|
Re: [perl #56986] 5.8.8: bug in substr() as lvalue?
On Wed, July 16, 2008 10:56 am, Ronald J Kimball wrote:
> On Wed, Jul 16, 2008 at 05:55:30AM -0700, Ulrich Windl wrote:
> Thank you for your bug report. Here's what the documentation for substr
> has to say:
>
> 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 "print(substr($foo,$a,$b)=$bar)" or
> "(substr($foo,$a,$b)=$bar)=$fud" (where $foo is changed via the sub-
> string assignment, and then the substr is used again), or where a substr()
> is aliased via a "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.
I blush whenever I read that (my) turgid paragraph and would still like
someone to make it more user-friendly.
> That definitely seems to apply to your code above. I'm not sure this is
> technically a bug, so much as unspecified behavior.
It was explicitly unspecified because we wanted to change it (and I believe
did so in 5.10; I only have a 5.11 right now, so I'm not sure at what point
the change was introduced). But neither 5.8.8 nor 5.11 produce the
results Ulrich is looking for. He's expecting:
perl -wle'$foo="abc"; print(substr($foo,0,1)="")'
to print "a", when in fact it prints "b" before 5.10 and "" after.
However, 4-arg substr does what he wants, replacing the range with
an empty string but returning its original contents:
$ perl -wle'$foo="abc"; print(substr($foo,0,1,""))'
a
Thread:
Ulrich Windl
Ronald J Kimball
Yitzchak Scott-Thoennes
David Nicol
|