in reply to shift doesn't DWIM

Where would the line be drawn? Would using map in "void context" spew its return value into @_ ?

DWIMery shouldn't venture into the "Do What I Wish" if doing so creates serious language inconsistancies, IMHO.

My memory may be incomplete or in need of refreshing, but I can't think of any subs that populate $_ as a default lvalue. Yes, there is the diamond operator in the special case of a while() loop, there is foreach(), the m// and s/// operators, and there are subs that utilize $_ as a default parameter. But there aren't any subs I can think of that populate $_ as though it were an lvalue.


Dave

Replies are listed 'Best First'.
Re^2: shift doesn't DWIM
by TimToady (Parson) on Jul 12, 2005 at 16:49 UTC
    Yes, and Perl 6 even the special while loop exception is going away. Instead you typically use topicalizers to set $_:
    given shift { do something with $_; }
    or
    for @ARGS { do something with $_; }
    And in particular, while on a filehandle turns into a (lazy) for:
    for =$fh { do something with $_; }
    In general, the only dwimming on void context in the standard Perl 6 dialect will be deciding whether to turn an unthrown exception (aka undef) into a thrown exception because someone forgot to check the return value.
Re^2: shift doesn't DWIM
by itub (Priest) on Jul 12, 2005 at 11:26 UTC
    Where would the line be drawn? Would using map in "void context" spew its return value into @_ ?

    split does exactly that, but that use is deprecated: http://perldoc.perl.org/functions/split.html . So I certainly doubt that such a feature will be added to map. ;-)

Re^2: shift doesn't DWIM
by tlm (Prior) on Jul 12, 2005 at 10:14 UTC

    My memory may be incomplete or in need of refreshing, but I can't think of any subs that populate $_ as a default lvalue. Yes, there is the diamond operator in the special case of a while() loop, there is foreach(), the m// and s/// operators, and there are subs that utilize $_ as a default parameter. But there aren't any subs I can think of that populate $_ as though it were an lvalue.

    Interesting question. I'm also having a hard time thinking of such functions...

    map and grep assign to an implicitly localized $_ (as does foreach if no loop variable is given), but not their results.

    The function readline clobbers $_ if it is in the test clause of a while loop:

    % date|perl -le '$_="x";while(readline *STDIN){printf uc};print"<$_>"' TUE JUL 12 01:56:04 EDT 2005 <>

    It wouldn't surprise me if there are more examples, but I can't think of any ATM.

    the lowliest monk