in reply to Re^2: Half-serious quest for prefix anonymous refs taking
in thread Half-serious quest for prefix anonymous refs taking

Update: As blazar so kindly pointed out, the (@) prototype is indeed meaningless. It just goes to show how useful it is to reread the docs every once in a while. I'd filed away the fact that prototypes let you do cool stuff and omit parens. Which they do, but not in the way that I understood it at the time I first read the docs.

Reread the docs from time to time. It's amazing what you can learn!

Back to my regularly scheduled foolishness...


The % and @ prototypes do the same thing--eat an entire list. Your suggestion that it should check for evenness is very good. It's in my battered old copy of Perl in a Nutshell, it works, but I don't see it in perlsub--interesting...

I used the % prototype despite its identicalness with @ just to clearly show my intentions--that is mkhref wants a hash. I'm not sure about that decision now, since it turns out to be a partially documented feature :).

The prototypes allow you to skip your parentheses, which makes the sub act more like a built-in. Since you said you wanted a built-in, I thought I'd fake it as well as can be done.

mkaref( 1..9 ); # becomes mkaref 1..9;

In a module, I'd have mkhref check for warnings, with $^W. If on, it should carp, that way the error refers to the "right" line of code.

sub mkhref (%) { if ( @_%2 ) { carp "Odd number of elements in mkhref" if $^W; return { @_, undef }; } else { return {@_}; } }


TGI says moo

Replies are listed 'Best First'.
Re^4: Half-serious quest for prefix anonymous refs taking
by blazar (Canon) on May 30, 2008 at 19:12 UTC
    The prototypes allow you to skip your parentheses, which makes the sub act more like a built-in. Since you said you wanted a built-in, I thought I'd fake it as well as can be done.

    I personally believe you'll find that you don't need prototypes at all, to allow one to skip parens:

    However I did hope it was clear enough that my "quest" is not for builtin (looking) functions, because in that case I can write them myself full well, but for builtin (hypothetical) syntax.

    --
    If you can't understand the incipit, then please check the IPB Campaign.

      You are absolutely right about the protoypes. D'oh. It's not the first time I've made a fool of myself.

      Prototyping built-ins with modules seems to be the thing to do in Perl. Look at the history of say(). Building your proposed changes and then advocating them seems to be the way to go.


      Update: Uh, ambiguous use of the word "protoyping" on my part. In paragraph one, I meant the Perl language feature. In paragraph two, I meant the act of making a test version of something.

      I agree with you about prototypes (the perl feature). They are so useful that in 8 years of using Perl, I've used them in actual working code 2 times. (Unless you count my newbie error of trying to prototype everything--then the count gets a bit higher. :) )

      If you want to advocate for this sort of built-in, build a library version of it that acts as much like you want the built in to work, and push it. If it becomes popular enough, the P5P/Perl6 crowd may decide that the deliciousness of it is irresistible and incorporate it. We now have a switch and also 'say'.


      TGI says moo

        I personally believe that the only useful prototypes (for me) are \@, then possibly \% although I can't remember having ever used it, and &. Oh, and the empty one: ().

        --
        If you can't understand the incipit, then please check the IPB Campaign.