Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Could some explain =~ vs !~ to me?

by Plankton (Vicar)
on May 08, 2004 at 02:12 UTC ( #351669=perlquestion: print w/replies, xml ) Need Help??
Plankton has asked for the wisdom of the Perl Monks concerning the following question:


This is has been bugging all day ...
#!/usr/bin/perl -w use strict; my $var = "WooHoo\n"; # I know that this will print ... print $var if $var =~ /^Woo/; # and I know that this will print ... print $var if $var !~ /^Hoo/; # and I think I understand whats # going on here ... $var =~ s/W/H/; print $var; # But I didn't expect this ... $var !~ s/H/W/; print $var;
Here's the ouput ...
Plankton@linux:~/perl/perlmonks> ./ Useless use of not in void context at ./ line 18. WooHoo WooHoo HooHoo WooHoo
It does complain a bit about void context ... but so what? Could somebody enlighten me?


Plankton: 1% Evil, 99% Hot Gas.

Replies are listed 'Best First'.
Re: Could some explain =~ vs !~ to me?
by Zaxo (Archbishop) on May 08, 2004 at 02:16 UTC

    $foo !~ $re is equivalent to !($foo =~ $re). It's that simple.

    After Compline,

Re: Could some explain =~ vs !~ to me?
by fizbin (Chaplain) on May 08, 2004 at 03:30 UTC

    It's easiest to understand if you realize that:

    $var !~ s/H/W/;

    is equivalent to:

    (not ($var =~ s/H/W/))

    That is, the only difference between !~ and =~ is the return value. The side effect of mutating the left hand side when evaluated is the same for both operators.

    That is, !~ followed by a s/// expression can screw up the contents of the left hand side just as well as though you had used =~

    This also explains the "null context" warnings. Since !~ has the same effect as =~ when you're ignoring the return value (as you are here), perl lets you know that you're probably doing something you didn't intend to do - people evaluate =~ just for the side effects all the time, but using !~ that way is a bit odd. Note that this code doesn't emit the same warning:

    #!/usr/bin/perl -w use strict; my $var = "WooHoo\n"; print $var if $var =~ /^Woo/; print $var if $var !~ /^Hoo/; if ($var =~ s/W/H/) {print "=~ succeeded\n";} else {print "=~ failed\n";} print $var; if ($var !~ s/H/W/) {print "!~ succeeded\n";} else {print "!~ failed\n";} print $var;

    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re: Could some explain =~ vs !~ to me?
by krusty (Hermit) on May 08, 2004 at 02:52 UTC
    Zaxo is right with his explanation, but what I don't understand is your output.

    I would expect the last line of output to be WooWoo

    Using your last few lines of code here's my output using perl 5.8.3
    Useless use of not in void context at line 7. WooWoo

    What happens is that the negated pattern bind operator substitutes the first capital "H" it finds in "WooHoo".
    I am evaluating whether the substitution was *not* successful, but since I do nothing with the result I get a the void warning. Since my string has changed I now print "WooWoo". I would have expected your code to print "WooWoo" too.

    I'm going to quit now because this sounds a bit too much like a Dr. Seuss book.


    Thanks duff. I see now what the problem is. In my code, I'm taking only a few snippets of the original code. If I take the whole code, I'm working on the modified string "HooHoo" at the time, and would need a /g modifier to get both "H"'s

      You must be doing something different than the OP if you get WooWoo. There are no /g modifiers on those substitutions and the string looks like HooHoo before he does the final substitution.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://351669]
Approved by blue_cowdawg
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2018-05-27 12:16 GMT
Find Nodes?
    Voting Booth?