c:\@Work\Perl>perl -wMstrict -MO=Deparse,-p -le
"my $data = '8000';
;;
if ($data eq '8000' or $data eq '0080') { print 'yes 1'; };
if ($data eq '8000' || $data eq '0080') { print 'yes 2'; };
"
BEGIN { $^W = 1; }
BEGIN { $/ = "\n"; $\ = "\n"; }
use strict 'refs';
(my $data = '8000');
if ((($data eq '8000') or ($data eq '0080'))) {
print('yes 1');
}
if ((($data eq '8000') or ($data eq '0080'))) {
print('yes 2');
}
-e syntax OK
... i have ... tried ... swapping the 8A-8F/A0 with eq to no avail.
Again with the experimentation:
c:\@Work\Perl>perl -wMstrict -le
"my $data = '8000';
;;
if ($data eq '8000' or $data eq '0080') { print 'yes 1'; };
if ($data eq '8000' || $data eq '0080') { print 'yes 2'; };
"
yes 1
yes 2
I don't know what code you tried when you used eq (the proper comparator for strings), but clearly it should work. At the same time, I would also agree with other respondents that a re-organization of the code to avoid an eye- and mind-bezoggling onslaught of if-statements would be very valuable just for the great increase in readability and maintainability I would expect it to yield.
i thought i read somewhere that its was better to use "||" instead of "or" and use "&&" instead of "and".
The problem with and and or is that these logical operators have such low precedence: their precedence is lower than , (comma), i.e., lower than whale dung. This was done so that a statement like
open my $fh, '<', $filename or die "opening '$filename': $!";
would work as expected. Unfortunately, such low precedence has some surprising side-effects, hence && and || are recommended for 'normal' usage, a recommendation I would endorse since I've stumbled over these odd side-effects myself.
Update: Here's an example of a precedence-induced difference between ! and not. This code produces warnings, but you may not be so lucky.
c:\@Work\Perl>perl -wMstrict -MData::Dump -le
"my ($x, $y) = (4, 5);
;;
my @ra = (1, 2, ! 3, $x, $y);
dd \@ra;
;;
my @rb = (1, 2, not 3, $x, $y);
dd \@rb;
;;
print qq{but \$x and \$y are still $x and $y};
"
Useless use of a constant (3) in void context at -e line 1.
Useless use of private variable in void context at -e line 1.
[1, 2, "", 4, 5]
[1, 2, ""]
but $x and $y are still 4 and 5
|