Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > odd regexp behaviour (?)

Reply
Thread Tools

odd regexp behaviour (?)

 
 
bengt wikenfalk
Guest
Posts: n/a
 
      10-10-2003
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)
 
Reply With Quote
 
 
 
 
Steve Grazzini
Guest
Posts: n/a
 
      10-10-2003
bengt wikenfalk <(E-Mail Removed)> wrote:
> 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;
}

--
Steve
 
Reply With Quote
 
 
 
 
Glenn Jackman
Guest
Posts: n/a
 
      10-10-2003
Steve Grazzini <(E-Mail Removed)> wrote:
> bengt wikenfalk <(E-Mail Removed)> wrote:
> > 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;
> }


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.

--
Glenn Jackman
NCF Sysadmin
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Odd Regexp Issue Kyle Heck Ruby 7 05-25-2007 01:35 PM
Odd behavior with odd code Michael Speer C Programming 33 02-18-2007 07:31 AM
Some more odd behaviour from the Regexp library David Veerasingam Python 4 10-20-2005 04:01 PM
Odd behaviour of regexp module David Veerasingam Python 3 07-14-2005 12:36 AM



Advertisments