Tuesday, September 18, 2007

scripting is fun!

I know nobody knows what the stuff below means, but yay. pretty!

#! /usr/bin/perl
use strict "subs";
use warnings;

$time_id = "65546"; #0x1000A pack/unpack can also interpret a string as a number in decimal
$emp_id = 0x77777777;
$item = "fresh bananas!!!";
$quan = 0x3456; # short should be 16 bits shorter
$urgent = 0x1;

printf ("\$time_id: %s, \$emp_id: %x, \$item: %s\n\$quan: %x, \$urgent: %x\n", $time_id, $emp_id, $item, $quan, $urgent);

$rec = pack( 'i i Z32 s2', $time_id, $emp_id, $item, $quan, $urgent);
$packed = unpack('h*', $rec); # convert to a printable string
printf "packed value: %s\n", $packed; # prints representation of items stored in little endian
@decode = unpack 'i i a32 s2', $rec;
printf "unpacked value: %#x %#x %s %#x %#x\n", @decode;# $decode[0], $decode[1], $decode[2], $decode[3], $decode[4];



foreach $key (sort keys (%ENV)) {
print "$key: $ENV{$key}\n";
}

print "PID: $$\t time: ", time, "\n"; # $$ is the current PID, 'time' is short for 'time()'
srand(time|$$);
for($i=0;$i<10;$i++)>
print "\n";


$SIG{'INT'} = 'handler'; # catch ctrl-c

sub handler {
local($sig) = @_; # first argument is signal name (pg 97)
print "caught SIG$sig -- exiting!\n";
exit(0);
}

sleep(10); # wait 10 seconds for an interrupt

No comments: