odd regexp behaviour (?)

B

bengt wikenfalk

Hi.

I wonder about the difference between =~ and = ~

I wrote a script with the following expression:


return $c if ($name = ~ /^OK$/);
($name is an array reference (ARRAY=0x....)
This works ! (if the array contains the string "OK", $c will be returned.

if I change the code to $name =~ ... it does not work however (which
actually makes me relieved ..)


Is this a standard feature ? Can anyone describe what happens here ?

(I'm running Perl 5.6 under cygwin)
 
S

Steve Grazzini

bengt wikenfalk said:
I wonder about the difference between =~ and = ~

"=~" is one operator. "= ~" is two.

#!/usr/bin/perl -l

$_ = "foo";
$str = "bar";

if ($str =~ /bar/) {
print "$str matched /bar/";
}

if ($str = ~ /foo/) {
print "\$str is $str";
print "That's ~1: ", ~1;
}
 
G

Glenn Jackman

Steve Grazzini said:
"=~" is one operator. "= ~" is two.

#!/usr/bin/perl -l

$_ = "foo";
$str = "bar";

if ($str =~ /bar/) {
print "$str matched /bar/";
}

if ($str = ~ /foo/) {
print "\$str is $str";
print "That's ~1: ", ~1;
}

Also:
$_ = '';
if ($str = ~ /foo/) {
print "\$str is $str";
print "That's ~0: ", ~0;
}

($str = ~ /foo/) means "assign to $str the bitwise negation of the
scalar result of matching /foo/ against $_". Note that both ~0 and ~1
are non-zero, hence true.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top