Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: why does push not default to $_?

by ptoulis (Scribe)
on Dec 06, 2008 at 00:48 UTC ( [id://728443]=note: print w/replies, xml ) Need Help??


in reply to why does push not default to $_?

The Camel book gives a good hint: "The underline is the underlying operand in certain operations."

These "certain operations" are the unary functions and the iterators like foreach,for, map{}, grep{}. The idea is that in a unary function there is only one operand and since these are used very often and Perl is Huffman encoded, it makes great sense to have a special name when this operand is not specified.

The push function is not unary and it would be a bad idea to include special names in its operands. Moreover, push mutates its first operand and it would really be a bad idea to change data and at the same time hide from the developer how the data is being changed.

Replies are listed 'Best First'.
Re^2: why does push not default to $_?
by LanX (Saint) on Dec 06, 2008 at 01:04 UTC
    > Moreover, push mutates its first operand and it would really be a bad idea to change data and at the same time hide from the developer how the data is being changed.

    Well for me, calling push with only one parameter is quite obvious. At least not less than print without parameter.

    Please, in which chapter did you find that quote in the camel book?

    Cheers Rolf

    UPDATE: here what I found in 2.9.3 Global Special Variables
    Here are the places where Perl will assume $_ even if you don't use it:
    
        *      Various unary functions, including functions like ord and int, 
     as well as all the file tests (-f, -d) except for -t, which defaults to STDIN.
        *      Various list functions like print and unlink.
        *      The pattern-matching operations m//, s///, and tr/// when used 
     without an =~ operator.
        *      The default iterator variable in a foreach loop if no other variable
     is supplied.
        *      The implicit iterator variable in the grep and map functions.
        *      The default place to put an input record when a <FH> operation's
     result is tested by itself as the sole criterion of a while test. Note that
     outside of a while test, this will not happen. 
    
    Mnemonic: underline is the underlying operand in certain operations.

    Well various doesn't sound very specific, and print and unlink are not unary.

      Well for me, calling push with only one parameter is quite obvious. At least not less than print without parameter.
      The difference is that print does not alter its operands but push does. Even ignoring these there is hardly a good reason to use $_ for push. For example, in iterators which set the $_ you might want to use something like: for(1..100) { push @a;}, but still you would be better going by push @a,(1..100);
      Please, in which chapter did you find that quote in the camel book?
      You can find the quote in Chapter 28-Special Names, page 659.
        which copy? I already updated in the previous post what I found in "Second Edition, September 1996.

        Please compare, in my copy, there is no mention about different handling of functions denpending on if they alter operands.

        Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://728443]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found