Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

by tye (Sage)
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        

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://575296]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-06-19 20:10 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.