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

In reply to Re^4: File::Temp: 2 interfaces get different results with Digest::MD5 and File::Compare by jkeenan1
in thread File::Temp: 2 interfaces get different results with Digest::MD5 and File::Compare by jkeenan1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.