in reply to Re: perl unpack and matching the unpacked data
in thread perl unpack and matching the unpacked data

isnt something like:
$data eq "8000" or $data eq "0080" the same as: $data eq "8000" || $data eq "0080";
i thought i read somewhere that its was better to use "||" instead of "or" and use "&&" instead of "and". anyways...like i said i have tried alot of different avenues, i have even tried only swapping the 8A-8F/A0 with eq to no avail.
i have been up for a while though so maybe i will take a break from it for a while and get some rest and come back to it with a fresh mind lol. I am looking forward to sharing the tool and the code to see any criticism or input on how to make it better, because honestly this is my first ever program ive done that actually amounted to anything, and its going to have a gui :D
everything else works great except for this snippet lol. but ill take a break and come back later.
also thanks for the input :)

Replies are listed 'Best First'.
Re^3: perl unpack and matching the unpacked data
by AppleFritter (Vicar) on Jul 17, 2014 at 22:32 UTC

    || and or (and, similarly, && and and) have the same semantics in principle, but the former has a higher precedence than the latter. eq has a higher precedence than either, so it doesn't make any difference which one you use there, but it can be important in other contexts.

    Operator Precedence and Associativity has more, as does chapter 3 of Programming Perl.

Re^3: perl unpack and matching the unpacked data
by AnomalousMonk (Archbishop) on Jul 17, 2014 at 22:34 UTC
    isnt something like:
    $data eq "8000" or $data eq "0080" the same as: $data eq "8000" || $data eq "0080";

    A little experimentation should reveal that Perl thinks it is:

    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

      ok im going to go ahead and give it another shot, and this time i will write it in this format:
      if ( $data eq "8000" || $data eq "0080" ) { $tid = "TEST"; $buf = "TEST SYSTEM"; } elsif ( $data eq "8000" || $data eq "0080" ) { $tid = "TOOL"; $buf = "TOOL SYSTEM"; } elsif.....

      i didnt try it exactly like this. what i tried was actually:
      elsif ( $data eq "8000" || "0080" ) { $tid = .....; }
      i think that will work. i will come back and update the thread when i come to a conclusion.
        ... what i tried was actually:
        elsif ( $data eq "8000" || "0080" ) { $tid = .....; }

        As you discovered, this syntax doesn't work — at least, not the way you expected: the truth value of the expression  $data eq '8000' is logical-ored with '8000'. Again, let Perl help you:

        c:\@Work\Perl\monks>perl -wMstrict -MO=Deparse,-p -le "my $data = '8000'; ;; if ($data eq '8000' || '0080') { print 'yes'; }; " BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict 'refs'; (my $data = '8000'); if ((($data eq '8000') or '0080')) { print('yes'); } -e syntax OK
        if ( $data eq "8000" || $data eq "0080" ) { $tid = "TEST"; $buf = "TEST SYSTEM"; } elsif ...
        and
        if ( $data eq "8000" or $data eq "0080" ) { $tid = "TEST"; $buf = "TEST SYSTEM"; } elsif ...
        are strictly equivalent (in this case). As others have commented, || and or have different precedence, but that doens not make any difference here. Personally I prefer to use the lower precedence or when grooping to Boolean expresions, but that's a matter of taste and that's debatable.
        if ( $data eq "8000" || "0080" )
        on the other hand is just wrong, although the compiler will assign a meaning to it. It is wrong in the sense that it is simply not doing what you intend: this is saying : if $data is "8000" or if "0080", which will be always true, because "0080" is a true value. So fix it as you said (with either of the two syntaxes shown at the beginning of this post) and your program should work properly.