I've noticed a discrepancy between the return value behavior of substr as an lvalue and as an rvalue. Consider,
The four argument form as rvalue evaluates to the replaced string, which is what we usually expect of it. As an lvalue, the assignments are associated right to left (as we also expect) but the substr call appears to be evaluated twice, first as an lvalue in assignment, then as an rvalue to produce the return value of that expression.$ perl -e's//foobar/;my $foo=substr($_,2,2,"rt");print $foo,$/' ob $ perl -e's//foobar/;my $foo=substr($_,2,2)="rt"; print $foo,$/' rt $
It seems that the rvalue behavior is more useful, because it allows the old data to be kept on the fly. That sort of entropy suppression is useful in functions like select.
The observed behavior may be inherent to perl's design for lvalue subs. The implementation of substr in pp.c is complicated, and I confess to not yet understanding it.
I wonder whether this is a bug, or a feature, or just a fact of life. What do you think?
After Compline,
Zaxo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: An Oddity of substr
by BrowserUk (Patriarch) on Oct 10, 2003 at 07:09 UTC | |
by dragonchild (Archbishop) on Oct 10, 2003 at 15:33 UTC | |
by BrowserUk (Patriarch) on Oct 10, 2003 at 22:17 UTC | |
by Zaxo (Archbishop) on Oct 10, 2003 at 07:30 UTC | |
Re: An Oddity of substr (old news :)
by tye (Sage) on Oct 10, 2003 at 06:40 UTC |