http://www.perlmonks.org?node_id=515326


in reply to Re: Autovivification of scalars in sub calls
in thread Autovivification of scalars in sub calls

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?

Be that as it may, where can I learn more about this sub-args-as-lvalues thing? I can't find documentation for it.

I agree that it is nice that being in an "lvalue context" induces autovivification, but I don't see the utility of regarding the arguments to a subroutine as lvalues. Granted, in some cases, like substr, the first argument can be thought of as being in an "lvalue context". Sort of. After a few drinks. But this seems too special a situation to warrant turning the arguments to a function into an "lvalue context".

the lowliest monk

Replies are listed 'Best First'.
Re^3: Autovivification of scalars in sub calls
by Roy Johnson (Monsignor) on Dec 08, 2005 at 18:47 UTC
    How is the argument in sleep( 3 ) an lvalue?
    In much the same way that \3 is. Not a very good answer, I know, but it's the best I can do. Perl makes @_ an array of aliases to the arguments, as you know. That's much like taking a reference: it requires Perl to consider the arguments in an lvalue context. That aliasing may not happen on builtin functions like sleep, though.

    Another example of aliasing is a for loop. It also provides an lvalue context:

    use warnings; use strict; my $x; # @$x; # This will die if uncommented for my $foo (@$x) { print "Gar!\n"; }
    While I'm certain that I read about the @_ lvalues phenomenon here on PM, I haven't been able to Super Search it up. tye mentions it in Re^5: Warnings not being thrown with DBI (nits).

    Caution: Contents may have been coded under pressure.
Re^3: Autovivification of scalars in sub calls
by Anonymous Monk on Dec 08, 2005 at 22:01 UTC
    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

      Perhaps also interesting is that lvalues remain lvalues even after passing them to the unary + operator.
      sub foo {} #foo(substr "hello", 10, 1); # fatal error as you pointed out #foo(+substr "hello", 10, 1); # I thought this would be a workaro +und foo(scalar substr "hello", 10, 1); # but you need something like this print "This only gets printed if the last line didn't cause a fatal er +ror\n";
      And testing to make sure:
      perl -e '+$a = "Hello, world!\n"; print $a' Hello, world!
      (I know it's documented that + does absolutely nothing, and I guess that does mean it doesn't convert lvalues to rvalues, but it's still somewhat surprising to me.)
Re^3: Autovivification of scalars in sub calls
by Perl Mouse (Chaplain) on Dec 08, 2005 at 23:02 UTC
    sleep is not a sub. sleep is a buildin. In this, it makes all the difference. Subs have @_, and their arguments are aliased to it. Hence the lvalue of arguments to subs - but not to buildins (with a few exceptions).
    Be that as it may, where can I learn more about this sub-args-as-lvalues thing? I can't find documentation for it.
    Very early in man perlsub, as soon as it starts talking about @_. The third paragraph of the description:
    Any arguments passed in show up in the array @_. There­ fore, if you called a function with two arguments, those would be stored in $_[0] and $_[1]. The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated (or an error occurs if it is not updatable). If an argument is an array or hash element which did not exist when the function was called, that element is created only when (and if) it is modified or a reference to it is taken. (Some earlier versions of Perl created the element whether or not the element was assigned to.) Assigning to the whole array @_ removes that aliasing, and does not update any arguments.
    Perl --((8:>*