Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
Do you know where your variables are?
 
PerlMonks  

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

by BrowserUk (Pope)
on Sep 28, 2006 at 02:44 UTC ( #575280=note: print w/ replies, xml ) Need Help??


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

I don't see why people have this trouble?

Which is clearer?

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

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re^4: Regex Or Die! (!unless)
Re^5: Regex Or Die! (!unless)
by tye (Cardinal) on Sep 28, 2006 at 07:23 UTC

    • 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        

      Lets not don't go there, ok?
Re^5: Regex Or Die! (!unless)
by codeacrobat (Chaplain) on Oct 02, 2006 at 14:22 UTC
    use Acme::don't;
    don't { print "This won't be printed\n" }; # NO-OP
    How about the TheDamian writes another one ;-)
    use Acme::not_do; not_do { print "This won't be printed\n" }; # NO-OP

      What a wonderful idea! I can add to my huge tally of cpan modules by uploading Acme::Ifn't.

      Then we can all become PBP complient and never use unless again.

      die "'eh up lad! Summat's wrong." ifn't $It'sAlright;

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-04-18 02:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls