.... ->
#!/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;
my $filename = $ARGV[0] || __FILE__;
open FH_PLAIN, '<', $filename or die $!;
#open FH_TIED, '<', $ARGV[1]; # try uncommenting this
tie(*FH_TIED, 'TieTest');
open FH_TIED, '<', $filename or die $!;
open FH_EEP, '<', $filename or die $!;
tie(*FH_EEP, 'TieTest');
open FH_EEP, '<', $filename or die $!;
my %handles = (
PLAIN => \*FH_PLAIN,
TIED => \*FH_TIED,
EEP => \*FH_EEP,
PEE => eval {
my $pee;
tie $pee, 'TieTest';
open $pee, '<', $filename or die $!;
$pee;
},
);
for my $title ( keys %handles ){
my $fh = $handles{$title};
for my $class (qw/Digest::Perl::MD5 Digest::MD5/) {
my $sum = eval { $class->new->addfile($fh)->hexdigest } || $@;
printf "%-7s%-19s%-32s\n", $title, $class, $sum;
seek $fh, 0, 0;
}
}
#~ use Devel::Peek qw/ Dump /; Dump( $_) for values %handles ; ## unin
+formative
__END__
$ perl perliotiehandle.pl
Odd number of elements in hash assignment at perliotiehandle.pl line 4
+4.
...tied read
...tied read
EEP Digest::Perl::MD5 623cf1cdac049dc6527cb0f28fc5aa76
EEP Digest::MD5 623cf1cdac049dc6527cb0f28fc5aa76
PLAIN Digest::Perl::MD5 623cf1cdac049dc6527cb0f28fc5aa76
PLAIN Digest::MD5 623cf1cdac049dc6527cb0f28fc5aa76
...tied read
...tied read
TIED Digest::Perl::MD5 623cf1cdac049dc6527cb0f28fc5aa76
TIED Digest::MD5 No filehandle passed at perliotiehandle.pl l
+ine 56.
PEE Digest::Perl::MD5 Digest::Perl::MD5 read failed: Bad file desc
+riptor at C:/citrusperl/site/lib/Digest/Perl/MD5.pm line 164.
Can't use an undefined value as a symbol reference at perliotiehandle.
+pl line 58.