Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Don't ask to ask, just ask
 
PerlMonks  

Re^5: Regex Or Die! (!unless)

by tye (Cardinal)
on Sep 28, 2006 at 07:23 UTC ( #575296=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Regex Or Die! (!unless)
in thread Regex Or Die!

  • I could care less.
  • Irregardless.

  • Don't speak unless you're spoken too.
  • Don't speak if not you're spoken too.

Well, the second one would be "Don't speak if you're not spoken to." I'm not advocating that people use improper Perl grammar. And, in proper English, they both seem pretty equally understandable. Many would prefer the former (especially if you drop the "you're", which would also make your second line clear and hence probably explains why you added it) simply because it is an oft-repeated phrase.

Even clearer would be "Only speak when spoken to." In Perl, that would be something like:

if( SpokenTo() ) { Speak(); }

which doesn't quite have the same meaning as the English phrase, at least stand-alone. A closer match would be:

sub Speak { return unless SpokenTo(); # ... }

which I don't find as clear as

sub Speak { return if ! SpokenTo(); # ... }

As I said, it is subtle. Jumping into a different language isn't likely the best way to try to understand it.

Certainly, I don't give much weight to a measurement of how likely a poor translation of a Perl construct into pidgin English is to sound like proper English. Programming constructs being similar to English can lower the learning curve in some ways, but if the people maintaining your code try to understand it by translating it word-for-word into English and then trying to understand that, then you've got worse problems than whether or not they use "unless". Only fairly trivial programming constructs make good sense when translating into English. That's why we use tools like charts and tables.

The "if" of most programming languages is similar to the "if" of English, but not identical, so conclusions drawn from one don't perfectly apply to the other. But there is significantly more difference between the "unless"es of Perl and English. And your choice of examples illustrates this nicely. Let's translate your sentences into Perl code:

DontSpeak() unless SpokenTo(); DontSpeak() if ! SpokenTo();

You see, I've never seen a subroutine / method named something like "DontSpeak". Most programming languages don't work that way. So I'll take a small detour on the route to explaining why and showing what that has to do with "unless".

The "if" of Perl is simply "if <present condition> then <do action (now)>" (though "present condition" could include "remembered state of past conditions"). The "unless" of English is most often used like "unless <potential future condition> don't <take action (future)>". But the "unless" of Perl is "unless <present condition> then <do action (now)>". And it doesn't really fit.

Tell someone "Speak unless spoken to" and you'll probably be able see their mental cogs spinning as they try to figure out what you really meant.

So, writing return unless SpokenTo(), then translating to English as "return unless spoken to" can easily beome, in your head, "don't return unless spoken to", because that is how "unless" is normally used in English.

If you could write:

unless( SpokenTo() ) dont { Speak(); }

Then "unless" might be less of a problem. But that pretty fundamentally goes against the way Perl works (it might work in a declarative language like Prolog).

The implied "not" in "unless" is subtle and there is also a customary second "not" that is subtle but missing from the Perl "unless". So when use "unless" in Perl code it is too easy to switch it to "if" in your head to avoid this dissonance or to add a "don't" to the action. And when you do this, it can be a long and frustrating process to find your back.

- tye        


Comment on Re^5: Regex Or Die! (!unless)
Select or Download Code
Re^6: Regex Or Die! (!unless)
by Anonymous Monk on Sep 28, 2006 at 07:29 UTC
    Lets not don't go there, ok?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2014-04-18 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (463 votes), past polls