Unless you use source filters (which I do not recommend mainly because of the complexity of parsing code, and the fact that the code perl compiles is not the same as the one you wrote) or design your own version of perl, you can't create a new operator.
You can however overload an existing one, and do something like $var ~~ @list where $var would have to be a blessed reference. $var ~~ @list may actually already do what you want if you use the experimental Smartmatch Operator (but that thing is so complex it's probably a bad idea to use it).
Or, if you're OK with the fact that in @list, $var is close enough to $var in @list you could do something like:
TIMTOWTDI of course, with or without List::Util, grep or Prototypes, that's just an example to show what can be done (and any and none are more effective than grep because they stop at the first match)use v5.20; use List::Util qw(any none); # Allows the first arg to be an array or a ref # If the second arg is absent, $_ is used instead sub in:prototype(+_) { any { $_[1] eq $_ } @{ $_[0] } } sub notin:prototype(+_) { none { $_[1] eq $_ } @{ $_[0] } } my @list = 'A'..'Z'; my $in = 'C'; say 'IN' if in @list, $in; $_ = '1'; say 'OUT' if notin \@list; say 'NOPE' if in @list, '2'; say 'NOPE' if notin @list, 'D';
Edit: updated the code because I had written { $_[1] } instead of { $_[1] eq $_ } and added some links
Edit: And of course, unless order is important, the more effective way to check that an element is present in a set is to use a hash instead of an array.
In reply to Re: Create a new operator, get LHS
by Eily
in thread Create a new operator, get LHS
by stevieb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |