develooper Front page | perl.perl5.porters | Postings from July 2012

Re: Smartmatch two cents (was... List::Util... when...)

Thread Previous | Thread Next
Ed Avis
July 3, 2012 01:59
Re: Smartmatch two cents (was... List::Util... when...)
Message ID:
Just a couple more thoughts on the suggestion of an 'in' operator
(which others besides me have proposed).

The current convention is that punctuation operators like == != < are
numeric, while alphabetic ones like eq ne lt do string comparisons.
An 'in' operator doing string comparison follows that convention.
While you could also have an equivalent in== operator, in my
experience that operation is much less common than the string version.

As in Python, 'in' could work for both arrays and hashes:

   $x in @array

   $x in %hash    # same as exists $hash{$x}

With the auto-dereferencing introduced in recent perls, the RHS could
be an array or hash ref.

   $x in $hash_ref    # same as exists $hash_ref->{$x}

I do not envisage the 'in' operator being part of some grand scheme
which will replace all functionality provided by smartmatch, but just
propose it as a simple operator which is useful in its own right and,
in my opinion, can make for clearer and more concise code.

Ed Avis <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About