Nathan Wiger wrote:
> 
> > : =~ has no real world equivalent, and in fact I don't know how to
> > : pronounce it in English so that $x =~ /a/ makes sense.
> >
> > Yes, that's all pretty much on the mark.
> 
> Not true, IMO. In math, =~ is used to indicate "rough equivalence".
> (Actually, the ~ goes on top of the =, but this is a minor difference
> considering you can't type it that way.) This same meaning I think is
> easily applied here:
> 
>    $x =~ /a/;   # $x is roughly equivalent to a
> 
> I use this mneumonic to clear up the difference between these regularly
> and people seem to be able to grasp it easily:

I don't like that because it implies a comparison operator instead of a
binding. It's fine in boolean context, but @l = $x =~ /(pat1)(pat2)/
doesn't really work.

> I think that =~ and !~ are very representative of their functions in
> matches. I'm actually surprised people find this so confusing,
> personally. eq and ne are far more confusing.

Hm. I've never had problems with this, although it does confuse me to go
back to the shell's [ ] (aka test) and have them be backwards. I mean
very different things by numeric vs string equality, and it's helpful to
be able to express the difference in the language directly. So it can
complain, if nothing else. And once you're aware of the existence of
both, the rule "eq is spelled with letters so it operates on strings"
makes it immediately obvious when to use which.

> Now, one could argue that since we already have m// and s/// there's no
> reason that you shouldn't just write:
> 
>    if ( $x == m/stuff/ ) { ... }
>    $str = s/$old/$new/gi;
> 
> Since s/// doesn't really return anything super-useful right now
> anyways. But if we're going to go down this road we should axe eq and
> ne, since this is something Perl should be able to figure out just using
> == and !=.

I don't understand your intent. What would s/// return? Or did you
intend $str == s///?

It has a very nice return value in boolean context. I often use

if (s/A/B/) { ... }

(read "if I was able to replace A with B")

Reply via email to