ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl5-porters
perl5-porters
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

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved