in reply to Checksum Help

I would have written that like this, keeping (and clarifying in the "Usage" message) the intention to allow multiple files to be done in one run. (Note -- on unix/linux/macosx for sure, don't know about "cmd.exe" -- when your command line is  prog_name *.txt or whatever, the shell does file name expansion, replacing "*.txt" with the list of all matching file names, before invoking the script, so @ARGV has that many elements in it.)
#!/usr/bin/perl use strict; use String::CRC32; my $Usage = "Usage: $0 file.name ...\n"; @ARGV or die $Usage; for my $file ( @ARGV ) { open( my $fh, "<", $file ) or die "Unable to open $file: $!\n"; my $crc = crc32( $fh ); close $fh; printf( "%08x ( %08x ) %s\n", $crc, 0xffffffff - $crc, $file ); }

I don't understand the relevance of 0xffffffff - $crc, but if it's useful at all, it helps to have fixed-width fields to the left of variable-width fields.

Replies are listed 'Best First'.
Re^2: Checksum Help
by TeamViterbi (Novice) on Jul 07, 2009 at 22:07 UTC
    Graff, so i tried using what you gave me, and i placed the file name where you have "file.name" However, whenever i run the script, terminal only prints out "Usage: file.name" and not the checksum. Any suggestions?
      If you have saved the script to a file called (let's say) "mycrc.pl", then you should see the following when you run it:
      mycrc.pl Usage: mycrc.pl file.name ... mycrc.pl * [list of all files in the current directory, with their crc values..]
      The same should happen if you run it like this:
      perl mycrc.pl perl mycrc.pl *
        Oh nice, thanks for your help. It works now. =]
        Oh and one more thing. It spits out two checksums, one is in paranthesis. What does each indicate?