Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Could some explain =~ vs !~ to me?

by Plankton (Priest)
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:

Friends,

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> ./tilda_q.pl Useless use of not in void context at ./tilda_q.pl line 18. WooHoo WooHoo HooHoo WooHoo
It does complain a bit about void context ... but so what? Could somebody enlighten me?

Thanks

Plankton: 1% Evil, 99% Hot Gas.

Comment on Could some explain =~ vs !~ to me?
Select or Download Code
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,
    Zaxo

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 test.pl 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.

    Cheers,
    Kris

    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.
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@/

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://351669]
Approved by blue_cowdawg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2014-08-20 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (120 votes), past polls