in reply to Implementing named infix operators with parser hook?

Dunno if this meets your needs, but have a look at how tobyink's Sub::Infix does it.

Edit: missing words.

  • Comment on Re: Implementing named infix operators with parser hook?

Replies are listed 'Best First'.
Re^2: Implementing named infix operators with parser hook?
by LanX (Saint) on Aug 12, 2018 at 15:15 UTC
    I think it doesn't, since I need most operators already. :/

    But thanks for reminding me.

    I new it from discussions here, but remembered it differently. (i.e. the operator needs to be blessed not the operands, which is cool)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Only just noticed this line of your comment:

      I think it doesn't, since I need most operators already. :/

      Not sure what you mean by it. Did you assume that using Sub::Infix takes over existing operators, so you can no longer use (for example) the | or / operators normally? Because that's not what it does. You can continue to use the existing Perl operators normally, with no slow down, but Sub::Infix will allow you to define additional operators like:

      use Sub::Infix; BEGIN { *between = infix { my ($number, $range) = @_: $number >= $range->[0] and $number <= $range->[1]; }; } if ($value <<between>> [1,10]) { ...; }

      The only limitation is that your infix operator can't be spelled just between. It needs to be |between|, /between/, or <<between>> (and depending on which you choose, you'll get different precedence).

        Yes you are right, I fell back into my misunderstanding that operators like / are overloaded, while you are making "between" a constant which returns objects blessed with a class with overloaded operators.

        Since / is only overloaded for that class it doesn't interfere with other parsing "objects".

        I doubt it's even possible in Perl to overload operators for unblessed objects like literals or scalar variables.

        (unless using the autobox module which hooks into the compiler via XS.)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice