Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Digest::MD5 addfile() w/ tied filehandle

by jdv (Sexton)
on Sep 01, 2015 at 21:18 UTC ( [id://1140719]=note: print w/replies, xml ) Need Help??


in reply to Digest::MD5 addfile() w/ tied filehandle

Following the suggestion of BrowserUk, I tried to write a bare-minimum example of this behavior. The code is below. The TieTest class is basically meant to remember an internal filehandle and then pass that along with the rest of the arguments through to the core Perl functions.

There are two interesting/questionable observations. First, with line 19 commented out as it is, Digest::MD5 croaks that it was not passed a filehandle. Maybe I'm doing something wrong here? The only way I have found to get around this is to open the filehandle on something before tie'ing it.

If I do that (by uncommenting line 19 and feeding the script two different filenames) the pure-Perl implementation acts on the last file opened, while the XS implementation is clearly reading from the first file opened on line 19 and not talking to the tied class (I added a debugging statement to the READ sub just to be sure). This does seem like a bug to me (although saying that out loud pretty much guarantees that I'll find an error in my own code within the next five minutes). Does anyone have any further thoughts on this? Otherwise I'll submit it as a possible bug as suggested.

Here is the test case

#!/usr/bin/perl package TieTest; sub TIEHANDLE { return bless {}, shift } sub OPEN { open my $fh, $_[1], $_[2]; $_[0]->{fh} = $fh; } sub READ { warn "...tied read\n"; read $_[0]->{fh}, $_[1], $_[2], + $_[3]; } sub SEEK { seek $_[0]->{fh}, $_[1], $_[2]; } package main; use strict; use warnings; use Digest::MD5; use Digest::Perl::MD5; open FH_PLAIN, '<', $ARGV[0]; #open FH_TIED, '<', $ARGV[1]; # try uncommenting this tie(*FH_TIED, 'TieTest'); open FH_TIED, '<', $ARGV[0]; my %handles = (PLAIN => \*FH_PLAIN, TIED => \*FH_TIED); for my $title (qw/PLAIN TIED/) { my $fh = $handles{$title}; for my $class (qw/Digest::Perl::MD5 Digest::MD5/) { my $sum = $class->new->addfile($fh)->hexdigest; printf "%-7s%-19s%-32s\n", $title, $class, $sum; seek $fh, 0, 0; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found