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

I don't know if I have dreamt this - I can't seem to find the information anywhere...

In my dream someone, could have been Damian Conway, explained how to make new operands in Perl. The magic involved was maybe Filter::Simple

Does this ring a bell to anyone? Or is it in fact only something I have dreamt?

L

Replies are listed 'Best First'.
Re: Home made operands
by moritz (Cardinal) on Nov 12, 2011 at 17:11 UTC

    Operands are arguments to operators. You can take any variable, constant or object you want and pass it to any operator you want.

    With overload you can give existing operators new meaning for some objects as operands.

    In Perl 6, you can make new operators simply by declaring them:

    sub postfix:<!>(Int $n) { [*] 1..$n } say 6!; # 120

    In Perl 5, there is no simple way. Filter::Simple and other source filters require much more work to declare new operators, and the result is generally rather brittle.

    Devel::Declare makes creating new syntax a bit more robust, but it's just a gradual improvement.

Re: Home made operands
by Plankton (Vicar) on Nov 12, 2011 at 16:55 UTC

    Does use overload sound like what you are looking for? As in ...

    ... use overload "-" => \&minus, "+" => \&plus, "*" => \&mult, "bool" => \&bool; ... sub plus { my $u = shift; my $v = shift; return new Vector2D ( $u->getx() + $v->getx(), $u->gety() + $v->gety() ); } ...
    But these would be homemade operators ;)
Re: Home made operands
by ikegami (Patriarch) on Nov 14, 2011 at 19:32 UTC

    feature::qw_comments makes a new keyword called qw. Ok, there's already one called qw, but the module completely replaces it. The same API can be used to make entirely new keywords.

    It uses an interface added to Perl in 5.14. This interface is much more resilient than source filters, and much more usable than Devel::Declare.

    It cannot be used to create symbolic (e.g. "+") operators, or anything but prefix operators/keywords (e.g. KEYWORD SOMETHING, not SOMETHING KEYWORD or SOMETHING KEYWORD SOMETHING).

    It can be used to introduce a section of code that's not Perl, just like m/ introduces a section of code that's a regexp, not Perl. For example, one could use this interface to create a keyword that indicates the start of a snippet of Lisp grammar.

Re: Home made operands
by Anonymous Monk on Nov 12, 2011 at 19:57 UTC

    Ah, as both of you understood I ment operator, but wrote something else. Thanks for the pointer to Perl6. Maybe this is what I so vaguely remember having read about

    L