Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have this script here that is designed to read through a bin file 32 bits at a time and add each 32 bits to the checksum. however, whenever i run this script, the print is never returned. Am i doing something wrong? Also, is the & symbol used to restrain the read to 32 bits?
sub cksum{ my $word_count; my $bias=0; my $checksum=0; my $srcfile="../vxWorks.dsu_flt-rom.bin"; my $buffer; open INF, $srcfile or die "\nCan't open $srcfile for reading: $!\n"; print "Cannot open file.\n"; binmode INF; $checksum = $bias; #while ($word_count>=0) while ( read (INF, $buffer, 32) #and print OUTF $buffer +# exit if read or write fails ) {$checksum = $checksum + $buffer}; #adding goes here $checksum = $checksum & 0xFFFFFFFF; print "This is the checksum: $checksum"; }
Thanks in advanced for your help.

Replies are listed 'Best First'.
Re: Print command not working in script
by jwkrahn (Abbot) on Jul 13, 2009 at 16:12 UTC

    You are reading 32 bytes at a time, not 32 bits.   And you are using addition on strings not numbers, which makes no sense.

Re: Print command not working in script
by jethro (Monsignor) on Jul 13, 2009 at 17:24 UTC

    Maybe you forgot to call the subroutine cksum. After I added that (and "use warnings;" at the start of the file) I got lots of output, warnings and expected prints

    Check out the unpack() function to get numbers out of the strings you read

Re: Print command not working in script
by spazm (Monk) on Jul 13, 2009 at 17:29 UTC
    You'll also likely want a newline at the end of your print statement.

    My shell often "eats" my output if it is only one unterminated line.