Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Spoken Perl: and || && or what?

by Zaxo (Archbishop)
on Jul 29, 2001 at 13:17 UTC ( [id://100663]=perlmeditation: print w/replies, xml ) Need Help??

I often use the logical operators && || and or as stand-ins for if (){};. Their short-circuit behavior is a handy idiom for conditionals.

I sometimes struggle with how to pronounce them. I always say 'and' and 'or' to myself and I know what I mean. How can they be said to others?

In English, 'do{} or die' is correctly understood by everyone. This extends to most ..or die; constructions in perl. The trouble is that it's the 'die' part that is recognised. The logical structure is swamped by the cliche.

A m// && print; sequence is second nature to perl programmers. What do you say when you read it out loud to nonperlers? To sugarcoat plain "match and print", I've used:

  1. "match forces print"
  2. "match means print"
  3. "match induces print"
  4. "match implies print"
None of those are very natural.

When the predicate is just a flag, it's helpful to make it a gerund or noun ( $production || $debugging && print;), but what verbs make the meaning of that clear in spoken language?

I wonder whether other languages provide more precision in these matters, too

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Spoken Perl: and && or what?
by tachyon (Chancellor) on Jul 29, 2001 at 17:39 UTC

    I like print if m// partly because it reads better in my mind. I don't like m// && print because it is very idiomatic and no shorter than print if m//.

    I like $var = shift || 'default' because it also reads naturally.

    If you have to explain idioms to non perlers use the common programing idioms of if/then/else...

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Spoken Perl: and && or what?
by Masem (Monsignor) on Jul 29, 2001 at 15:27 UTC
    IMO: m// && print reads as "match and then print", with the implication that if you can't match, you don't print. This goes along with "m// || die" as "match or then die".

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Re (tilly) 1: Spoken Perl: and && or what?
by tilly (Archbishop) on Jul 30, 2001 at 07:19 UTC
    A moral. Saying that given idiom is natural to Perl programmers is dangerous. TIMTOWTDI, and the ways that you are used to people using Perl may not be used by others.

    Personally the only time I intentionally use the short ciruiting behaviour of && or || as conditionals is within a condition of an if. ie I will write:

    if ($cond_1 && $cond_2) { # Do something }
    which I (of course) don't think about in the same way that I would in a different context. Instead I (like tachyon) use the:
    print if /match/;
    form because I like the way it reads better.
Re: Spoken Perl: and && or what?
by synapse0 (Pilgrim) on Jul 29, 2001 at 13:39 UTC
    My thoughts on it would be to use such code in terms of IF .. THEN .. with variance for further understanding.. As an example, using your examples, the m// && print; bascially breaks down into "if blah matches then print", if it were used in an if like if (m// && print) { do whatever; } it would turn into "if blah matches and print succeeds, then do whatever".
    In your second example, it turns into "if production is false then if debugging is true, print or else dont print" or if it was used as an if like  if ($production || $debugging && print) { do whatever; } then it turns into "if production is true or debugging is true and print succeeds, then do whatever"... (I'm under the assumption here that precedence makes that statement ($production || ($debugging && print)) )
    basically, they are still if then statements..
    -Syn0
      If you're going to basically translate the entire meaning of the statement into another language, as opposed to transliterate the individual symbols, check out Conway's Roman module for ideas. He wrote a thing to program in Latin. All the punctuation marks and stuff are gone, and it uses only grammatically correct Latin sentences.

        For the uninformed, that's Lingua::Romana::Perligata, and it's a sight to behold.

        If (by some chance) you don't speak Latin (what do they teach them in these schools?), then you might be interested in Lingua::tlhInganHol::yIghun. Unfortunately, I can't read the release status, but it's definitely not on CPAN yet. ;-)



        If God had meant us to fly, he would *never* have given us the railroads.
            --Michael Flanders

Re: Spoken Perl: and && or what?
by VSarkiss (Monsignor) on Jul 30, 2001 at 02:31 UTC

    IMHO your examples read better if you use pronouns also: "match this and print it". (The implied $_ in the print is the "it".)

    I agree that the code should be "easy to pronounce"; that is, it should emphasize what you're trying to do: is the pattern match more important or the print? In other words, if I was doing something grep-like, I'd probably write: m/.../ and print;which I'd pronounce "match something and print it". But if I was doing something like reading a command stream, I'd be more likely to use: print if /.../;and read it as "print it if (you're supposed to)".

Re: Spoken Perl: and && or what?
by John M. Dlugosz (Monsignor) on Jul 30, 2001 at 02:56 UTC
    I'd like to point out that Perl already has synonyms and and or, so I'd naturally use those to read out loud.

    However, I think there are so many problems with reading code out loud that it's not much of an issue: what about parenthesis that are not pronounced at all?

    Say pseudocode out loud, during design sessions with a team. But real code works best of the white board.

Re: Spoken Perl: and && or what?
by mugwumpjism (Hermit) on Jul 29, 2001 at 17:24 UTC

    Match allows print?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://100663]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-24 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found