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})
| [reply] [d/l] [select] |
|
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! | [reply] [d/l] |
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.
Cheers,
lin0
| [reply] |
|
| [reply] |
Re^2: interpretation of "neither...nor"
by Fletch (Bishop) 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).
| [reply] [d/l] |
Re^2: interpretation of "neither...nor"
by FunkyMonk (Chancellor) 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};
| [reply] [d/l] [select] |
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!
| [reply] [d/l] [select] |
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.8.8.32.11.32"
| [reply] [d/l] [select] |
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
| [reply] |
Re^2: interpretation of "neither...nor"
by aquarium (Curate) on Jun 18, 2007 at 01:58 UTC
|
print "Editor not defined" or $ENV{EDITOR} or $ENV{VISUAL}
untested
the hardest line to type correctly is: stty erase ^H
| [reply] [d/l] |
|
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. | [reply] [d/l] |