Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: interpretation of "neither...nor"

by cgmd (Beadle)
on Jun 17, 2007 at 03:41 UTC ( #621650=perlquestion: print w/replies, xml ) Need Help??
cgmd has asked for the wisdom of the Perl Monks concerning the following question:

Knowledgeable Monks...

Making my way through Vincent Lowe's "PERL Programmers - Interactive Workbook", I'm having trouble agreeing with the solution to a "neither...nor" coding issue.

The assignment posed in the text: "Write an instruction that prints a message if neither of the environment variables EDITOR nor VISUAL is defined."

The text's suggested answer to this:

print "no editor found\n" if ! (defined $ENV{EDITOR} || defined $ENV{VISUAL});
By my interpretation, the correct operator should be &&, what say you, Monks?


Replies are listed 'Best First'.
Re^2: interpretation of "neither...nor"
by Zaxo (Archbishop) on Jun 17, 2007 at 03:46 UTC

    See DeMorgan's laws in formal logic. The solution is correct because (as truth tables will reveal),

    ! (defined $ENV{EDITOR} || defined $ENV{VISUAL})
    is equivalent to,
    (! defined $ENV{EDITOR} && ! defined $ENV{VISUAL})

    After Compline,

      Thank you, Zaxo, for that reference.

      Following up, I have found:

      Augustus De Morgan originally observed that in classical propositional logic the following relationships hold:

      not (P and Q) = (not P) or (not Q)

      not (P or Q) = (not P) and (not Q)

      This explains why the following are equivalent instructions:

      print "no editor found\n" if ! (defined $ENV{EDITOR} || defined $ENV{VISUAL}); print "no editor found\n" if (!defined $ENV{EDITOR} && !defined $ENV{VISUAL});

      Thank you for setting me straight! This has been very helpful!

Re^2: interpretation of "neither...nor"
by lin0 (Curate) on Jun 17, 2007 at 04:01 UTC

    Hi cgmd,

    Since you already got the answer to your question, the only thing I could add is a recommendation to have a careful look at the article on Truth Tables in the Wikipedia.



      Thank you for the Wikipedia link. It was quite helpful!

Re^2: interpretation of "neither...nor"
by Fletch (Chancellor) on Jun 17, 2007 at 03:50 UTC

    I say read again, harder this time. ! ( defined $ENV{EDITOR} && defined $ENV{VISUAL} ) will be false iff both are defined; the suggested solution will only be true iff both are not defined (i.e. neither the one nor the other are defined, which is just what it said to begin with).

Re^2: interpretation of "neither...nor"
by FunkyMonk (Canon) on Jun 17, 2007 at 07:41 UTC
    There's only a few occasions when I find unless more readable than if. This is one of them:

    print "no editor found\n" unless defined $ENV{EDITOR} || defined $ENV{VISUAL};
Re^2: interpretation of "neither...nor"
by naikonta (Curate) on Jun 17, 2007 at 10:51 UTC
    By my interpretation, the correct operator should be &&, what say you, Monks?
    What prevents you from doing so?
    print "no editor found\n" if (!defined $ENV{EDITOR} && !defined $ENV{VISUAL});
    But, I would say,
    die "no editor found\n" unless my $editor = $ENV{EDITOR} || $ENV{VISUAL}; print "predefined editor: $editor\n";
    If you have some interpretation, just run the code to see if the result meets what you expect. Otherwise, trying to "read" the code more verbally is one way to have some grasp whenever I get a little puzzled. "If this is false: EDITOR is undefined, or, VISUAL is undefined then I have no predefined editor set". I keep repeating that until it fits mentally into my understanding. In this particular example, I usually end up with: "OK, I'll do it with unless....".

    You can also talk to your teddy bear or your monitor in this process.

    Update: (18-06-2007) I found bobf's True or False? A Quick Reference Guide also quite a reference with many examples and enlightening tables.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re^2: interpretation of "neither...nor"
by strat (Canon) on Jun 17, 2007 at 09:34 UTC

    I'd use or and not instead of || && ! because the long names:

    • are better readable, and especially the ! is often "hidden behind other code".
    • have the lowest precedence which might avoid errors if the expressions get more complicated, especially if there are .. ... ?: = += -+ and so on , => within them. Especially stuff like open FH, $filename || die "Error: $!"; is fatal.

    So I'd write it the following way, although it is a little bit longer:

    print "no editor found\n" unless defined $ENV{EDITOR} or defined $ENV{VISUAL};

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8."

Re^2: interpretation of "neither...nor"
by Cody Pendant (Prior) on Jun 17, 2007 at 13:41 UTC
    I suppose you could phrase that in english as, first "print 'no editor found' if you don't get a true from testing this list ..." and then you can consider the || to be no different from a comma.

    The code will test the first one. Is it false? Maybe it is, but there's a second test, so we keep going, because the second one might be true, and so on.

    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re^2: interpretation of "neither...nor"
by aquarium (Curate) on Jun 18, 2007 at 01:58 UTC
    how about
    print "Editor not defined" or $ENV{EDITOR} or $ENV{VISUAL}
    the hardest line to type correctly is: stty erase ^H
      Won't work. As per perlop this parses to
      ((print "Editor not defined") or $ENV{EDITOR}) or $ENV{VISUAL}
      hence "Editor not defined" will always be printed and the result will always be true.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://621650]
Approved by Zaxo
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2017-05-28 09:39 GMT
Find Nodes?
    Voting Booth?