Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

  • 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        


In reply to Re^5: Regex Or Die! (!unless) by tye
in thread Regex Or Die! by pileofrogs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found