in reply to Prototypes and hand-written MMD

Your question was nearly incomprehensible because you start out with a code example that is simply a syntax error. My first guess was that you were asking a Perl6 question but didn't bother to say so.

The problem you'll run into is more likely that you can't have two different prototypes for class() (plus a bunch of other problems). But ignoring your first proposal, sub class(&), (since it is probably incompatible with your primary question) you are incorrect in your thinking on how to make class 'Foo' is { ... } work...

The prototype for sub class shouldn't be ($&) since you aren't putting a block as the second argument to class() (tho, & only really works for first arguments anyway). Your second argument to class() is a plain expression that returns a code reference. The prototype of is() is what handles the bare block, not the prototype of class() at all.

The next problem is that you don't want to put a comma between 'Foo' and is{...}. That means you've got that extra-vague syntax that /might/ get interpretted as indirection object syntax (but might not, and is pretty easy to break besides).

So, Perl might interpret your code as 'Foo'->class( is { ... } ) (but it might not, depending).

But my primary advice is to turn back now. Please don't make another module that tries to fool the perl interpretter into understanding some non-Perl5 syntax. Let the people reading Perl5 code and expecting to find Perl5 code understand the code. Attempts to invent alternate syntax always end up being quite fragile; that is, they often confuse the computer in addition to confusing the human.

Next you can try source filters. I hope you have the sense not to, however.

- tye        

Replies are listed 'Best First'.
Re^2: Prototypes and hand-written MMD (Ah! No!!)
by dragonchild (Archbishop) on Jan 10, 2006 at 14:08 UTC
    Sorry about that. I should have posted a link to How does strict determine if something is legally declared? which explains where I get that syntax and what I'm doing. I'm not asking a P6 question in disguise - it really is a P5 question. And, I don't (currently) intend on releasing this code to CPAN - this is just a fun little "Yet Another Perl OO Framework" for my own edification of some squirrelly P5 corners I haven't explored yet. So, to better phrase the question . . .

    I have a function defined as so:

    sub foo (&) { my ($sub) = @_; ... } # Usage: foo { ... };
    I want to also support the following usage: foo 'Bar' is { ... }; which requires a ($$) prototype. (You are correct in that it's $$, not $&.)

    Is there a way to do it using the same function name or do I have to use two different functions?


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      You'd need two different functions. The prototype is to help perl parse your code and perl doesn't provide for conditional prototypes.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊