Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ~~ in perl do?

Tags:

operators

perl

I saw this code and couldn't understand what it was doing it compiled succesfully.I searched for ~~ OPERATOR but no luck.

 unless ($1 ~~ @tables) {
 push @tables, $1;
 }
like image 734
user2377119 Avatar asked Feb 24 '26 23:02

user2377119


1 Answers

Reference: http://perldoc.perl.org/perlop.html#Smartmatch-Operator

First available in Perl 5.10.1 (the 5.10.0 version behaved differently), binary ~~ does a "smartmatch" between its arguments.

The ~~ operator compares its operands "polymorphically", determining how to compare them according to their actual types (numeric, string, array, hash, etc.) Like the equality operators with which it shares the same precedence, ~~ returns 1 for true and "" for false. It is often best read aloud as "in", "inside of", or "is contained in", because the left operand is often looked for inside the right operand. That makes the order of the operands to the smartmatch operand often opposite that of the regular match operator. In other words, the "smaller" thing is usually placed in the left operand and the larger one in the right.

like image 135
Aziz Shaikh Avatar answered Feb 26 '26 19:02

Aziz Shaikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!