[Israel.pm] Is there an operator like IN in perl

Mikhael Goikhman migo at homemail.com
Thu Mar 5 07:51:54 PST 2009


On 04 Mar 2009 16:09:57 +0200, Shlomi Fish wrote:
> 
> On Wednesday 04 March 2009 15:27:04 Amir E. Aharoni wrote:
> > 
> > use 5.010;
> >
> > my @beatles = qw(John Paul George Ringo);
> >
> > if ('George' ~~ @beatles) {
> >     say 'George is in Beatles';
> > }
> > if ('Amir' ~~ @beatles) {
> >     say 'Amir is in Beatles?!'; # This is not printed
> > }
> 
> My problem with this code is that it's hard to tell whether ~~ will do
> numeric or string comparison.

There is no such problem in this code. The comparison here is always
string comparison, and it is easy to tell, since the element here is
explicitly string.

If the element of this operator is a variable instead ($element), then
it is still possible to force one or another comparison explicitly:

  $element     ~~ @list   # smart DWIM comparision, depends on operands
  "$element"   ~~ @list   # force string comparision
  $element + 0 ~~ @list   # force numeric comparision

So the only real problem of this powerful operator is that it requires
perl 5.10. Meaning, for example, I can't use it in my projects yet.

Regards,
Mikhael.

-- 
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'



More information about the Perl mailing list