On Fri, Jan 9, 2009 at 16:40, Mike McClain <mmccl...@nethere.com> wrote:
snip
>    I appreciate your taking the time to respond and having been
> reading your responses for several months now have no doubt that
> you know what you're talking about.
>    I'm still not clear however about what's going on.
>    As you can see from the above calculations what's being returned
> by Digest::MD5->md5_hex is neither the md5 sum of the module nor the
> concatenation of the module and the argument (my file md5_hex).
>    Would you mind explaining further or pointing me to further reading
> that might straighten out this feeble old mind?
snip

You seem to be under the impression that the argument to
Digest::MD5::md5_hex is a file name.  The argument is a scalar holding
the data to perform md5 on.  So Digest::MD5::md5_hex("foo") will give
you the MD5 of the data "foo".  When you say
Digest::MD5->md5_hex("foo") you are really saying
Digest::MD5::md5_hex("Digest::MD5", "foo") so you will get the MD5 of
the string "Digest::MD5" concatenated with the string "foo".  If you
want to perform an MD5 on a file you must either get the data from
that file into a scalar or use the objected oriented interface's
addfile method call.  The method call is more efficient because the
whole file does not need to be in memory at once (it just reads in one
block at a time).


#!/usr/bin/perl

use strict;
use warnings;

use Digest::MD5;

print "Calling md5_hex with 'Digest::MD5':                    ",
        Digest::MD5::md5_hex("Digest::MD5"),  "\n",
        "Calling md5_hex with 'Digest::MD5' in a different way: ",
        Digest::MD5->md5_hex(), "\n",
        "Calling md5_hex with 'Digest::MD5foo':                 ",
        Digest::MD5::md5_hex("Digest::MD5foo"),  "\n",
        "Calling md5_hex with 'Digest::MD5' and 'foo':          ",
        Digest::MD5->md5_hex("foo"), "\n";

#store this file as data in $data
seek DATA, 0, 0;
my $data = join '', <DATA>;

#get an md5 of this file another way:
open my $fh, "<", $0
        or die "could not open $0: $!";
my $md5 = Digest::MD5->new;
$md5->addfile($fh);

print "Calling md5_hex on this program:                       ",
        Digest::MD5::md5_hex($data), "\n",
        "Calling md5_hex on this program another way:           ",
        $md5->hexdigest, "\n";

__DATA__

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to