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
2
votes
4 answers

Can I replace the binding operator with the smartmatch operator in Perl?

How can I write this with the smartmatch operator (~~)? use 5.010; my $string = '12 23 34 45 5464 46'; while ( $string =~ /(\d\d)\s/g ) { say $1; }
sid_com
  • 24,137
  • 26
  • 96
  • 187
2
votes
1 answer

Why does smartmatch not work when dereferencing an arrayref on the left-hand side?

I am currently reading Intermediate Perl from O'Reilly and am attempting to to do one of the exercises. I am new to references in Perl, so I hope that I am not misunderstanding something and coding this exercise incorrectly. However, I have tried to…
Burzum619
  • 143
  • 3
  • 13
1
vote
3 answers

Can I use smart matching, ~~, in Test::More's cmp_ok?

I am testing a function that returns an array. The array could be different depending on the environment but it will always have at least one constant value (the one that I want to test). As I am using Perl 5.12, I can use the smartmatch operator to…
Pablo Marin-Garcia
  • 4,151
  • 2
  • 32
  • 50
1
vote
1 answer

What is the proper way to search array using Smart Match?

I'm new to programming much less Perl; I'm having difficulty with searching an array I've made from an external text file. I'm looking for a simple way to check if the user entry is located in the array. I've used the Smart Match function before…
Bennett
  • 585
  • 1
  • 12
  • 24
1
vote
1 answer

Syntax compilation problems with Smart Match in Perl?

I seem to be having an issue compiling a perl script of mine on an older version of perl that I was hoping someone could help me with. The system that I originally wrote and compiled the perl file on without issue is using perl v5.16.3, the system…
cycloxr
  • 387
  • 1
  • 2
  • 15
1
vote
1 answer

Why does smartmatch return a different value when I use the range operator on the righthand side instead of an array?

Why does this smartmatch return false $value = 5; print "true" if $value ~~ (1..5); while this one returns true? $value = 5; @match = (1..5); print "true" if $value ~~ @match;
aidan
  • 9,310
  • 8
  • 68
  • 82
1
vote
1 answer

Why does smartmatch against keys %h fail and give an "Argument isn't numeric" warning?

In the following code, why does the first smartmatch fail to match and give the warning Argument "two" isn't numeric in smart match, while the second smartmatch works as expected (it matches)? use strict; use warnings; use feature 'say'; my %h =…
Rekr
  • 35
  • 3
0
votes
1 answer

How to make a column value as an number range for smartmatch

I have the txt file with the column value look like 85806534..85893402 49011742..49029143 114352846..114428174 I want to do the smartmatch. I put thses values into @array[0] if ($line ~~$array[0]){do something here} How can I make $array[0] as a…
Victor.H
  • 157
  • 1
  • 8
0
votes
3 answers

Why is this regex from a text file not working with smartmatch?

I use smartmatch to check whether a string matches a regex pattern. It stopped working after I decided to store the regex in a text file. my $str = '123456, some text.'; my $regex = qr/^\d+, some text\.$/; print "Regex: $regex\n"; At this point,…
Georg
  • 1,078
  • 2
  • 9
  • 18
0
votes
2 answers

Does Perl's smartmatch work against arrays (or arrayrefs) of mixed strings and compiled regexes?

I'm hoping to use Perl's smart-matching to do look-ups against an array that contains both strings and compiled regexes: do_something($file) unless ($file ~~ [ @global_excludes, $local_excludes ]); (Both the @global_excludes array and the…
lzc
  • 919
  • 7
  • 16
1
2