Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Perl to cook you coffee

by jwkrahn (Abbot)
on Feb 07, 2011 at 07:33 UTC ( [id://886611]=note: print w/replies, xml ) Need Help??


in reply to Perl to cook you coffee

if ($device eq undef) { die "Usage: <app> /dev/hidrawN"; exit; }

undef is neither a string nor a number.    You have to use the defined function to test whether a variable is defined or not.    Or you could just test whether @ARGV was empty or not.    die exits from the program so using exit there is superfluous.

@ARGV or die "Usage: <app> /dev/hidrawN";


my $report = ord(substr($data, 1, 1)); my $status = ord(substr($data, 2, 1)); my $unit = ord(substr($data, 3, 1)); my $exp = ord(substr($data, 4, 1)); my $lsb = ord(substr($data, 5, 1)); my $msb = ord(substr($data, 6, 1)); my $weight = ($msb * 255 + $lsb) / 10; if($exp != 255 && $exp != 0) { $weight ^= $exp; }

If you used unpack you could extract all your data in one statement and you wouldn't have to calculate the $weight value yourself, which is calculated wrong because you should be multiplying by 256, not 255.

my ( $report, $status, $unit, $exp, $weight ) = unpack 'xCCCCS', $ +data $weight /= 10; if ( $exp != 255 && $exp != 0 ) { $weight ^= $exp; }

Replies are listed 'Best First'.
Re^2: Perl to cook you coffee
by kirillm (Friar) on Feb 07, 2011 at 08:45 UTC

    Ah, uh, die() and exit()... I'll let 'em know about the suggestions.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://886611]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-26 05:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found