in reply to Re: Create a new operator, get LHS (indirect method + autoload)
in thread Create a new operator, get LHS
Nice :). Your post inspired me to try something with Lvalue subroutines, I think this works quite well:
use v5.20; use Operator qw{in}; use Variables qw{X Y Z}; my @list = 1..10; (X, Y) = (1, 11); say X." in @list" if X in @list; say Y." not in @list" unless Y in \@list; Z = 'A'; say Z." in list" if Z in ['A'..'C'];
package Variables; use v5.20; our %V; sub import { shift; my $package = caller; no strict "refs"; for my $var (@_) { *{"$package::$var"} = sub:lvalue { @_ ? $_[0]->($V{$var}) : $V{$va +r} }; } } 1;
package Operator; use v5.20; use List::Util qw{any none}; use Exporter 'import'; our @EXPORT_OK = 'in'; sub in(+) { my $list = shift; return sub { any { $_ eq $_[0] } @$list }; } 1;
1 in 1 2 3 4 5 6 7 8 9 10 12 not in 1 2 3 4 5 6 7 8 9 10 A in list
Edit: fixed the broken link for lvalue subroutines thanks to shmem.
Edit: $V{$var} is what I meant, not $V::{$var}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Create a new operator, get LHS (indirect method)
by LanX (Saint) on Aug 28, 2015 at 12:43 UTC | |
by Eily (Monsignor) on Aug 28, 2015 at 12:57 UTC |