I'm thinking of how to make a decent FileHandle class for 5.002.
Preliminary work seems ok, but I have some questions about the
code below:
1) Why is my FileHandle::DESTROY called twice per object, and why
does its base type change en route? I didn't something could
even *HAVE* a basetype other than one of SCALAR, HASH,
ARRAY, GLOB, REF, CODE.
2) How can we jimmy in reasonable names so that the warn()s look
nice?
3) Maybe open could on a null could fill in a glob with the
name of the opened-to handle?
--tom
use FileHandle;
use Socket;
use strict;
{
package FileHandle;
use Carp;
sub gensym {
no strict 'refs';
local *{'FileHandle::DEMI_ANON_GLOB'}; # YANETUT
return \delete $FileHandle::{DEMI_ANON_GLOB};
}
sub new { bless &gensym => 'FileHandle' }
sub DESTROY {
my $self = shift;
my $type = ref $self;
carp "\tFH DESTROY of $type: $self";
if (defined fileno $self) {
warn "\t\tCLOSING";
close $self;
}
}
}
############################
{
package Socket;
no strict 'vars';
@ISA = qw(FileHandle);
}
############################
package main;
sub HR { STDOUT->print("\n" . ("=" x 30) . "\n\n") }
{
my $fh = new FileHandle;
open ($fh, "/etc/motd");
print <$fh>;
}
HR;
{
my $fh = new FileHandle;
open ($fh, "cat -n /etc/motd |");
print <$fh>;
close $fh;
}
HR;
{
my $sock = new Socket;
socket($sock, PF_INET, SOCK_STREAM, scalar getprotobyname 'tcp') || die "socket: $!";
}
HR;
{
my $p1 = new FileHandle;
my $p2 = new FileHandle;
pipe($p1,$p2) || die "pipe: $!";
}
HR;
print "ok, done\n";
HR;
Preliminary work seems ok, but I have some questions about the
code below:
1) Why is my FileHandle::DESTROY called twice per object, and why
does its base type change en route? I didn't something could
even *HAVE* a basetype other than one of SCALAR, HASH,
ARRAY, GLOB, REF, CODE.
2) How can we jimmy in reasonable names so that the warn()s look
nice?
3) Maybe open could on a null could fill in a glob with the
name of the opened-to handle?
--tom
use FileHandle;
use Socket;
use strict;
{
package FileHandle;
use Carp;
sub gensym {
no strict 'refs';
local *{'FileHandle::DEMI_ANON_GLOB'}; # YANETUT
return \delete $FileHandle::{DEMI_ANON_GLOB};
}
sub new { bless &gensym => 'FileHandle' }
sub DESTROY {
my $self = shift;
my $type = ref $self;
carp "\tFH DESTROY of $type: $self";
if (defined fileno $self) {
warn "\t\tCLOSING";
close $self;
}
}
}
############################
{
package Socket;
no strict 'vars';
@ISA = qw(FileHandle);
}
############################
package main;
sub HR { STDOUT->print("\n" . ("=" x 30) . "\n\n") }
{
my $fh = new FileHandle;
open ($fh, "/etc/motd");
print <$fh>;
}
HR;
{
my $fh = new FileHandle;
open ($fh, "cat -n /etc/motd |");
print <$fh>;
close $fh;
}
HR;
{
my $sock = new Socket;
socket($sock, PF_INET, SOCK_STREAM, scalar getprotobyname 'tcp') || die "socket: $!";
}
HR;
{
my $p1 = new FileHandle;
my $p2 = new FileHandle;
pipe($p1,$p2) || die "pipe: $!";
}
HR;
print "ok, done\n";
HR;