Interesting. I knew that some builtins, like pos and keys, can be lvalues, but not that arguments to subs were. Come to think of it, I can't even say that I know what the expression "arguments to subs are lvalues" means. How is the argument in sleep( 3 ) an lvalue?
Arguments to subs can be assigned to; they're lvalues in that sense.
Therefore, you get these ugly situations:
my $var=3;
mysleep($var); # these two lines are fine
mysleep(3); # but this one causes an error!!!
sub mysleep {
sleep($_[0]);
$_[0]=10;
}
Or this ugly situation...:
$y=substr("hello",10,1);
foo($y);# this runs, but substr() generates warnings because the subst
+r() is outside the string
foo( substr("hello",10,1) ); # this is a fatal error!
Why is it a fatal error? Well, because an invalid substr() is used in a lvalue context. Why is it an lvalue context? Because the subroutine *could* modify the value, and perl isn't smart enough to check to see if it does or not! Worse yet, substr() fails silently on such a fatal error, after generating the *same* warning that's nonfatal in a non-lvalue context. ***SIGH***
I've made it my policy to avoid lvalues() where possible, especially after that substr() issue killed our production code, and wasted a few hours of my time!
--
Ytrew Q. Uiop |