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:

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';
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)

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.