Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: why does push not default to $_? (simple)

by LanX (Saint)
on Dec 05, 2008 at 09:41 UTC ( [id://728237]=note: print w/replies, xml ) Need Help??


in reply to Re: why does push not default to $_? (simple)
in thread why does push not default to $_?

Am I understanding you right, the prototyope mechanism of builtins like push can't distiguish between missing parameters and empty parameters. But some special functions like "print" and "splice" have a proper non-prototype-based interface? (*)

I'm not really missing that feature, it's just a meditation about design and orthogonality.

IMHO, it would be much easier to prog if perl had more axiomatic rules instead of a bunch of special exeptional cases. Something like "if the last obligatory list @ or scalar $ in a prototype is ommitted in the code, $_ will be included instead throwing an error".

Or as more flexible approach "you can use "@_" and "$_" in prototypes to flag parameters which default to $_ if missing" ²

anyway, perl6's solution to flag these functions with a trailing . and making them methodes of an invisible $_ seems reasonable! (eg  .print; .push @a;)

Cheers Rolf

UPDATES:

(*) Must be! That's why prototype returns undef for print and split, showing they are not overridable!

DB<10> use Data::Dumper DB<11> print Dumper prototype "CORE::split" $VAR1 = undef; DB<12> print Dumper prototype "CORE::print" $VAR1 = undef; DB<13> print Dumper prototype "CORE::push" $VAR1 = '\\@@';

(²) But I don't know if it makes sense to push a global like $_ on the stack...

Replies are listed 'Best First'.
Re^3: why does push not default to $_? (simple)
by JadeNB (Chaplain) on Dec 05, 2008 at 22:30 UTC
    Or as more flexible approach "you can use "@_" and "$_" in prototypes to flag parameters which default to $_ if missing"
    Is this a claim (that you can already do this) or a suggestion? If it's a claim, then note that _ is already a valid character in prototypes for a scalar argument defaulting to $_, as you want. If it's a suggestion, then I think that having a prototype syntax like @_ would be very confusing (besides being ambiguous, given that it conflicts with a currently valid, although useless, prototype), since @_ already has a quite different meaning.

    By the way, notice that Params::Validate has a very rich defaults syntax for this sort of thing. In particular, you can do something like

    validate_pos 1, ( { default => $_ } ) x ( @_ - 1 )

    UPDATE: Oops, that last snippet doesn't make any sense. Indeed, as I think about it further, what does it mean to have an array of values, all of which default to $_ if absent? I think it's best to stick with the case of allowing explicitly listed parameters to default:

    validate_pos 1, ( { default => $_ } ) x 57

      it's a meditation!

      > that _ is already a valid character in prototypes for a scalar argument defaulting to $_, as you want.

      well, couldn't find it in the docs perlsub ... ups ...I was scanning the text und examining the examples and tables, but after a textsearch for "_" there is a somehow hidden phrase:

      As the last character of a prototype, or just before a semicolon, you can use _ in place of $ : if this argument is not provided, $_ will be used instead.

      wow, it's already implemented!

      So its possible to override CORE::push with sub push(\@_;@), which will default to $_ if the list is omitted!

      That means there is no technical reason that it's not already implemented this way, it's just Larry's decission.

      UPDATE Thats wrong, see following post...

      This thread is really instructive ... thanx! : )

      Cheers Rolf

        I tried a prove of concept of overiding push, such that a missing second parameter defaults to $_.
        use subs 'push'; my @a; $_="x"; # push @a; # Error: "Can't use an undefined value as an ARRAY referen +ce ..." print @a; # Override! sub push (\@_;@) { CORE::push( @{+shift},@_) ; } push @a; print @a; # prints "x" @b=("y","z"); push @a,@b; print @a; # prints "2"
        So it's possible to use push defaulting to $_, but a second Array to push will be forced into scalar context! 8(

        So the prototypesymbol _ doesn't work with parameterlists ...

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found