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

Hello all,
I am trying to capture the output from a program but all I get is a null value. Here is my code and the results:
use Digest::MD5 qw(md5_hex); use warnings; $checksum = 0; $filename = "compress_logs.pl"; $fingerprint = md5_hex($filename); print "$fingerprint Perl Gen\n"; $checksum = `md5sum compress_logs.pl`; print "$checksum: Linux Gen\n"; [root@zzz me]# /usr/bin/perl test.pl b2b0b5ab494414bbbbe6625e247de247 Perl Gen 933be8d9a84230aa8162932102dd0bf0 compress_logs.pl : Linux Gen
As you can see the md5sum program does execute but $checksum is set to a null value. I have also tried the pwd command with the same result. Any ideas?
You may have also noticed the the checksums shown above do not match. They are both calculated using MD5. Does anyone know why this is?
Kelly

Edit: g0n - repaired tags

Replies are listed 'Best First'.
Re: Problem returning values from external programs
by kyle (Abbot) on May 08, 2007 at 14:56 UTC

    The checksums don't match because in Perl you're computing the MD5 of the string in $filename (see the documentation for Digest::MD5).

    I don't see that $checksum is set to a null value, as you say. It seems to me that it's set to "933be8d9a84230aa8162932102dd0bf0 compress_logs.pl\n". Maybe you want to chomp it before you use it?

Re: Problem returning values from external programs
by Krambambuli (Curate) on May 08, 2007 at 16:32 UTC
    Looking on the second screen of Digest::MD5's doc, wherefrom this is derived, I guess the below code is what you want:
    #!/usr/bin/perl use strict; use warnings; use Digest::MD5 qw( md5_hex ); my $filename ="compress_logs.pl"; open( FILE, '<', $filename) or die "Cannot open file $filename: $!\n"; binmode( FILE ); my $digest1 = Digest::MD5->new->addfile( *FILE )->hexdigest; print "$digest1 $filename Perl Gen\n"; my $digest2 = `md5sum compress_logs.pl`; chomp( $digest2 ); print "$digest2 Linux Gen\n";