(Ovid) Re: 'or' vs '', '&&' vs 'and'
by Ovid (Cardinal) on Apr 01, 2002 at 17:48 UTC
|
When you spell out those kewords ("||" as "or", "&&" as "and"), you are using a keyword with a lower precedence, thus allowing you to skip parentheses:
open (THEFILE, ">$file") || die "Could not create $file - $!\n";
# versus:
open THEFILE, ">$file" or die "Could not create $file - $!\n";
I like spelling them out as they make reading the expression more natural (because it's a word instead of a symbol) and they allow you to avoid a lot of excessive parentheses.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats. | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: 'or' vs '', '&&' vs 'and'
by Parham (Friar) on Apr 01, 2002 at 19:11 UTC
|
i was taught to use "or" and "and" instead of || and && and was never told why. Later on i discovered what the rest of the monks have already stated. With "or" and "and" you have the advantage of including or not including parentheses whereas with "||" and "&&" they must be included. | [reply] [Watch: Dir/Any] |
Re: 'or' vs '', '&&' vs 'and'
by vek (Prior) on Apr 01, 2002 at 18:46 UTC
|
Way to go starting a religious war :)
- rdfield++.
Not wishing to get involved in said religious war I'll not state my preference :-). All I will say is that you need to be careful if you decide not to spell out the keywords - make sure you don't forget those parens otherwise you won't catch failures:
# a subtle bug...
open THEFILE, ">/somedir/somefile" ||
die "Could not create /somedir/somefile - $!\n"
If you do that, you'll never know something bad happened. | [reply] [Watch: Dir/Any] [d/l] |
Re: 'or' vs '', '&&' vs 'and'
by mrbbking (Hermit) on Apr 01, 2002 at 18:35 UTC
|
"Better" is (usually)? a matter of opinion.
For the examples you gave, I like to use 'and' and 'or' instead of '&&' and '||', and I use parentheses.
The parens allow me to forget which set has higher precedence.
And I like the 'and' and 'or' because they are more natural. | [reply] [Watch: Dir/Any] |
Re: 'or' vs '', '&&' vs 'and'
by brianarn (Chaplain) on Apr 01, 2002 at 19:21 UTC
|
The difference is the precedence, which can be handy not just for opening files. I still prefer to use parens when I open cause I like the look of it better, but then use an or as well.
You can also mix them to make some checks easier, seeing as || has a higher precedence than and, saving some parentheses.
if ($a || $b and $c)
if (($a or $b) and $c)
if(($a||$b)&&$c)
All of these lines are equal.
The parens add readability, but hey, maybe you like the visual of only one set of parens. :) TMTOWTDI
~Brian | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: 'or' vs '', '&&' vs 'and'
by Juerd (Abbot) on Apr 01, 2002 at 20:55 UTC
|
A lot of people have already mentioned the precedence, in particular with open. An example implementing both would be:
open FILE, $file || 'default.txt' or die "Can't open: $!";
If you don't know how something will be parsed, use B::Deparse, and have it add a lot of parenthesis using -p:
perl -MO=Deparse,-p -e'open FILE, $file || "default.txt" or die "Canno
+t open: $!";'
Which outputs (without color):
(open(FILE, ($file || 'default.txt')) or die("Cannot open: $!"));
-e syntax OK
Hope this helps understanding.
U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: 'or' vs '', '&&' vs 'and'
by rdfield (Priest) on Apr 01, 2002 at 17:50 UTC
|
Way to go starting a religious war :) Personally I use 'and' and 'or' for readability. Purists will no doubt point out the slight difference in precedence between the two forms, but for all practical purposes (AFAIK) there's no real difference.rdfield | [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: 'or' vs '', '&&' vs 'and'
by rbc (Curate) on Apr 01, 2002 at 19:12 UTC
|
For opening files I like to do this ...
if( !open( FH, "<$file") ) {
die "Cannot open $file :$!\n";
}
... just reminds me of C.
| [reply] [Watch: Dir/Any] [d/l] |
|
I much prefer the 'unless' version of this as for me (who needs new glasses) I can sometimes miss the ! so I prefer.
unless(open( FH, "<$file") ) {
# Error code, logging, bits, bobs (etc etc)
die "Cannot open $file :$!\n";
}
As a matter of personal taste I think this is more natural (to me).. unless this do this ;P | [reply] [Watch: Dir/Any] [d/l] |
Re: 'or' vs '', '&&' vs 'and'
by seanbo (Chaplain) on Apr 01, 2002 at 21:08 UTC
|
For readability, I prefer the following:
open (THEFILE, ">$file") or die "Could not create $file - $!\n";
The parens are not needed, but I think it helps to keep things clean. Readable code is always helpful!!
perl -e 'print reverse qw/o b n a e s/;' | [reply] [Watch: Dir/Any] [d/l] |
Re: 'or' vs '', '&&' vs 'and'
by Popcorn Dave (Abbot) on Apr 01, 2002 at 20:47 UTC
|
Okay, the way I was taught was to do it was:
my $file = 'somefile.txt';
die "Could not open file" unless open (THEFILE, ">$file");
Is this now preferable to the or/|| or it just a matter of personal preference?
How much of the religious war flames does this fan? :) | [reply] [Watch: Dir/Any] |