Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^4: File::Temp: 2 interfaces get different results with Digest::MD5 and File::Compare

by jkeenan1 (Deacon)
on Aug 29, 2021 at 21:47 UTC ( [id://11136193]=note: print w/replies, xml ) Need Help??


in reply to Re^3: File::Temp: 2 interfaces get different results with Digest::MD5 and File::Compare
in thread File::Temp: 2 interfaces get different results with Digest::MD5 and File::Compare

Corion,

Based on your suggestion, I developed the workaround below. The trick seems to have three parts to it:

1. binmode $FH, ':raw': binmode the tempfile(handle) before writing to it. (I suspect that on Unix, we can get away without the ':raw', but whatever.)

2. close $FH: close the tempfile(handle) after writing to it and before calling hexdigest on it.

3. Ignore File::Compare::compare() for now. (I don't need for my real-world problem, anyway.)

#!perl use 5.14.0; use warnings; use Carp; use Data::Dumper; use Digest::MD5; use File::Temp qw( tempfile ); use Test::More; sub hexdigest_one_file { my $filename = shift; say "Filename: $filename"; my $state = Digest::MD5->new(); open my $FH, '<', $filename or croak "Unable to open $filename for + reading"; $state->addfile($FH); close $FH or croak "Unable to close $filename after reading"; return $state->hexdigest; } my $basic = 'x' x 10**2; my @digests; my ($fh1, $t1) = tempfile(); binmode $fh1, ':raw'; for (1..100) { say $fh1 $basic } close $fh1 or croak "Unable to close $t1 after writing"; push @digests, hexdigest_one_file($t1); my $t3 = File::Temp->new( UNLINK => 0); binmode $t3, ':raw'; for (1..100) { say $t3 $basic } close $t3 or croak "Unable to close $t3 after writing"; push @digests, hexdigest_one_file($t3); say Dumper [ @digests ]; cmp_ok($digests[0], 'eq', $digests[1], "Same md5_hex for $t1 and $t3"); done_testing();

Why this works I do not know. I think this is, at the very least, a deficiency in the File::Temp documentation and will file a bug report on it.

Thank you for your assistance.

Jim Keenan
  • Comment on Re^4: File::Temp: 2 interfaces get different results with Digest::MD5 and File::Compare
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11136193]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found