perldoc -f substr:my $result = substr($string,2,6,''); print $result,"\n"; # Result is 234567
An alternative to using substr() as an lvalue is to specify the replacement string as the 4th argument. This allows you to replace parts of the EXPR and return what was there before in one operation, just as you can with splice().
I agree that the assignment way is pretty weird and contraintuitive, but I guess there is a reason... such as the possibility to use both ways depending on what you want to acheive?
Update: I just realized that it makes a bit more sense when you think about this:Which sets first $y to 42, then $x to the value of $y. In which case, you set the substring to a value, then that value is propagated to the next variable. And when you assign a substring to an empty string, it shrinks, and then the substring from position 2 to 7 is returned (which is now what was in positions 8 to 13). Heh. Still pretty weird anyways, but there is method to the madness. :)$x = $y = 42;
In reply to Re: Bug with substring return value?
by Dog and Pony
in thread Bug with substring return value?
by shotgunefx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |