Questions tagged [smartmatch]

Smartmatch (denoted by ~~) is a Perl operator that does "smart" comparisons of operands with possibly differing types (e.g. string and array).

Smartmatch (denoted by ~~) is a Perl operator that does "smart" comparisons of operands with possibly differing types (e.g. string and array).

For example:

'foo' ~~ @array

returns true if @array contains any elements equal to the string foo.

Smartmatch was first introduced in Perl 5.10.0 and changed significantly in 5.10.1. As of Perl 5.18.0, smartmatch is experimental and will almost certainly change or be removed completely in a future release. Using it in its current form is discouraged.

25 questions
62
votes
3 answers

Perl 5.20 and the fate of smart matching and given-when

I just installed Perl 5.18, and I get a lot of warnings like this, given is experimental at .\[...].pl line [...]. when is experimental at .\[...].pl line [...]. Smartmatch is experimental at C:/strawberry/perl/site/lib/[...] line [...]. Looking…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
23
votes
4 answers

What does " ~~ " mean in Perl?

In an SO answer daxim states: @array ~~ $scalar is true when $scalar is in @array to which draegtun replies: From 5.10.1+ the order of ~~ is important. Thus it needs to be $scalar ~~ @array How about a small primer on ~~ with link(s) to…
CW Holeman II
  • 4,661
  • 7
  • 41
  • 72
18
votes
3 answers

How fast is Perl's smartmatch operator when searching for a scalar in an array?

I want to repeatedly search for values in an array that does not change. So far, I have been doing it this way: I put the values in a hash (so I have an array and a hash with essentially the same contents) and I search the hash using exists. I don't…
Karel Bílek
  • 36,467
  • 31
  • 94
  • 149
9
votes
3 answers

Why does multiple use of `<( )>` token within `comb` not behave as expected?

I want to extract the row key(here is 28_2820201112122420516_000000), the column name(here is bcp_startSoc), and the value(here is 64.0) in $str, where $str is a row from HBase: # `match` is OK my $str = '28_2820201112122420516_000000…
chenyf
  • 5,048
  • 1
  • 12
  • 35
9
votes
4 answers

Why does smartmatch return different values depending on the order of the operands?

I have an array for which the following test returns true: 1 ~~ @a And yet, the following test returns false: @a ~~ 1 I read in Learning Perl that the placement of the values on either side of the smartmatch operator doesn't matter, but obviously…
Ikram Hawramani
  • 157
  • 1
  • 8
8
votes
3 answers

Why does smartmatch return true when comparing array slices that should be different?

The following script smart-matches slices of two arrays. At the start, both arrays are the same and I'm getting reasonable results. Then I change one of the arrays and smart-match two new slices, but it still says that the slices are identical.…
askucins
  • 93
  • 7
8
votes
1 answer

Signatures smartmatching misunderstanding

While reading and trying signature smartmatching I run into something strange. Executing the following smartmaching signature pairs: my @sigs = :($a, $b), :($a, @b), :($a, %b); my @signatures_to_check = :($, $), :($, @), :($, %); my $c = 0; for…
jakar
  • 1,701
  • 5
  • 14
7
votes
1 answer

Why does smartmatch return false when I match against a regex containing slashes?

I'm trying to match a simple string against a regular expression pattern using the smartmatch operator: #!/usr/bin/env perl use strict; use warnings; use utf8; use open qw(:std :utf8); my $name = qr{/(\w+)/}; my $line = 'string'; print "ok\n" if…
abra
  • 585
  • 1
  • 5
  • 12
4
votes
3 answers

Why does @array ~~ LIST return false even though @array contains the same elements as LIST?

I have @a = (1,2,3); print (@a ~~ (1,2,3)) and @a = (1,2,3); print (@a == (1,2,3)) The first one is the one I expect to work, but it does not print anything. The second one does print 1. Why? Isn't the smart matching operator ~~ supposed to…
Qiang Li
  • 10,593
  • 21
  • 77
  • 148
4
votes
1 answer

How can I check that a string matches all patterns in an array using smartmatch?

I want to check that a string matches multiple regex patterns. I came across a related question, which Brad Gilbert answered using the smartmatch operator: my @matches = ( qr/.*\.so$/, qr/.*_mdb\.v$/, qr/.*daidir/, qr/\.__solver_cache__/, …
Starfish
  • 697
  • 1
  • 6
  • 18
3
votes
2 answers

The smartmatch operator is not working as expected

Why the smartmatch operator ~~ says that 0 is not in (0, 5..100)? print ((0 ~~ (0, 5..100)) ? "Y" : "N"); N Test it here.
Ωmega
  • 42,614
  • 34
  • 134
  • 203
3
votes
2 answers

Is ~~ a short-circuit operator?

From the Smart matching in detail section in perlsyn: The smart match operator short-circuits whenever possible. Does ~~ have anything in common with short circuit operators (&&, ||, etc.) ?
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
3
votes
2 answers

Why is @array ~~ $number different from @array == $number?

According to Programming Perl, using smartmatch with "any" on the left and a number on the right checks numeric equality: ------------------------------------------------------------------------------ | Left | Right | Description | Like (But…
d.k
  • 4,234
  • 2
  • 26
  • 38
2
votes
2 answers

How do I use Perl's smart matching to match many patterns at once?

I was trying to follow some examples to use smart matching in the following piece of code, but failed (nothing was filtered out). How can I use smart matching here to match against multiple regexes at once? my $regexes_to_filter_a = ("tmp", "temp",…
David B
  • 29,258
  • 50
  • 133
  • 186
2
votes
1 answer

Perl: Use smartmatch op to search string against array of values

Consider a string, like a response header: HTTP/1.1 404 Not Found. I'm curious if you could use the combination of the smartmatch (~~ or double tilde) operator and regex to search for incomplete, or a subset, of matches. my $head = q{HTTP/1.1 404…
vol7ron
  • 40,809
  • 21
  • 119
  • 172
1
2