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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.