in reply to Create a new operator, get LHS

How would one get the LHS (in the above case var) so I can use it in whatever implementation I decide to use?

If you want a real infix in operator, you will have to hack the perl parser, namely perly.y, toke.c and related files.

You could overload an existing infix operator with a in subroutine, but in your programs you would still use the overloaded operator (not in), which then just behaves as in, and your $var has to be blessed to that end.

There's a notation which at least looks similar to infix - method call:

$var->in(@list)

- but $var has to be blessed here, too, into the package into which the in method has been compiled:

package in { sub new { bless \$_[1],$_[0] } sub in { my $t = shift; scalar grep { $$t eq $_ } @_ } }; my $var = in->new(7); print "yup\n" if $var->in( 0 .. 8 ); __END__ yup

Not very tingly. Sorry about that.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: Create a new operator, get LHS (lexical method)
by LanX (Saint) on Aug 26, 2015 at 13:37 UTC
    > There here's a notation which at least looks similar to infix - method call:

    > $var->in(@list)

    > ...

    > Not very tingly. Sorry about that.

    so why not

    $var->$in(@list) ??

    use warnings; use strict; my $in = sub { my $x=shift; return scalar grep {$x eq $_} @_ }; if ( 5 ->$in (0..9) ) { print "bin ich drin oder was?"; } if ( 'x' ->$in (0..9) ) { print "mennoooooo... :("; }

    /usr/bin/perl -w /tmp/in.pl bin ich drin oder was? Compilation finished at Wed Aug 26 15:42:03
    (diabolic laughter)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      (diabolic laughter)

      /me concurs. Didn't know that was even possible. Thanks, LanX. Learned something new today.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        Sadly this approach has a serious flaw (apart from the ugly syntax) ... lexical variables are not easily exported.

        :(

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!