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

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.

Replies are listed 'Best First'.
Re^5: perl unpack and matching the unpacked data
by AnomalousMonk (Archbishop) on Jul 18, 2014 at 01:10 UTC
    ... 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
Re^5: perl unpack and matching the unpacked data
by Laurent_R (Canon) on Jul 18, 2014 at 06:24 UTC
    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.