Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Why doesn't Perl default (more)

by misterperl (Pilgrim)
on Aug 19, 2020 at 13:34 UTC ( [id://11120901]=perlquestion: print w/replies, xml ) Need Help??

misterperl has asked for the wisdom of the Perl Monks concerning the following question:

Why isn't
push @a;
equivalent to
push @a,$_;

also why isn't

push;
the same as:
push @_,$_;

..randomly,it seems that sometimes good ole $_ works as in:

split /,/; # knows what to default to...

Might as well have defaulted everything to something; allowing code to be more concise, succinct, and economical. It would be awesome to have some PerVars that we could use to set the default scalar, array, and hash..

Larry what were you thinking? When we discussed this at OSCON, you seemed to favor defaults as a default ?

Replies are listed 'Best First'.
Re: Why doesn't Perl default (more)
by tobyink (Canon) on Aug 19, 2020 at 17:21 UTC

    If push @a; were currently a syntax error, then it would make sense for it to mean push @a, $_; because doing something useful is better than a compilation error. But push @a; isn't a syntax error — it actually does do something — it pushes the empty list onto the array.

    Pushing an empty list might seem like a no-op which could never be useful, but if you think that, you're forgetting about tied arrays.

Re: Why doesn't Perl default (more)
by haukex (Archbishop) on Aug 19, 2020 at 13:39 UTC
    Why isn't push @a; equivalent to push @a,$_;

    Likely because that could cause ambiguity whether my @x=(); push @y, @x; should push $_ or not.

    Sure, in theory it's possible for the compiler to differentiate the two. But I doubt that'll get changed in Perl 5, where the focus is on backwards-compatibility.

    randomly,it seems that sometimes good ole $_ works

    When $_ works is generally well-documented for each function.

Re: Why doesn't Perl default (edited)
by LanX (Saint) on Aug 19, 2020 at 14:50 UTC
    > push @a;

    Actually that was one of my first questions here.

    (EDIT found it: why does push not default to $_?)

    My guess is that it's because it's taking multiple args via push @a, LIST , and not a single one push @a, EXPR °

    > allowing code to be more concise, succinct, and economical.

    Nah, that's not true, because the defaults in Perl are not orthogonal.

    You have to check for every operator and built-in how it's implemented.

    especially the scalar behavior is very diverse.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    EDIT

    °) While it's still technically possible, this might be logically to confusing.

    And symmetry would require a void ;shift or ;pop to automatically set $_ , which would most certainly break things!

Re: Why doesn't Perl default (more)
by perlfan (Vicar) on Aug 19, 2020 at 16:57 UTC
    push @_,$_;
    This is almost never what you want and contradicts the shift @_ idiom for processing subroutine parameters.

    Rather than judging what push @a; does against what you consider the proper implicit behavior, I think it's more appropriate to judge against what unshift does in this case.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found