Cool/useful short examples of Perl?

Peter Corlett abuse at cabal.org.uk
Wed Jun 8 13:44:45 BST 2011


On 8 Jun 2011, at 13:00, Abigail wrote:
[...]
> I'd rather go for sacking people that don't know the difference 
> between 
>    if (something) { ... }
> and
>    unless (!something) { ... }
> Or does everyone think they are always equivalent?

Is this a trick question?

I would expect it to be equivalent if "something" is an expression with higher precedence than "!". If not, I'd be fascinated to know what the counterintuitive behaviour is and why somebody thought it was a good idea. (And also how I've managed to treat them as equivalent for years and not get burned.)

For "something" being arbitrary text, no. if($foo && $bar) and unless(!$foo && $bar) are of course not equivalent.

I do like "unless". It's more visible than "!". I like it enough that I'll invert the sense of an expression using De Morgan's law and use unless to eliminate those oft-missed plings. Avoiding misreading trumps avoiding ugliness.





More information about the london.pm mailing list