D
Dan Rawson
I have a tiny class (copied almost verbatim from the perltoot pages) which has some AUTOLOAD methods plus one other.
When I run the attached, it dies with "Can't access DESTROY field . . . ." at the very end. What am I doing wrong??
TIA . . . .
Dan
#!/usr/bin/env perl -w
package myTest;
use Carp;
our $AUTOLOAD;
use strict;
my %config = (
recursive => 0, # Bool
overwrite => 0, # Bool
);
sub new()
{
my $class = shift;
my $self = {
_permitted => \%config,
%config,
};
bless $self, $class;
return $self;
}
sub AUTOLOAD()
{
my $self = shift;
my $type = ref($self) || die;
my $name = $AUTOLOAD;
$name =~ s/.*://;
unless (exists $self->{_permitted}->{$name})
{
croak "Can't access $name field in object of class $type";
};
if (@_)
{
return $self->{$name} = shift;
}
else
{
return $self->{$name}
}
}
sub show_config()
{
my $self = shift;
print "Recursive: ".($self->recursive() ? "True" : "False")."\n";
print "Over-write: ".($self->overwrite() ? "True" : "False")."\n";
}
1;
package MAIN;
my $fred = myTest->new();
my $mary = myTest->new();
$mary->recursive(1);
$mary->show_config();
When I run the attached, it dies with "Can't access DESTROY field . . . ." at the very end. What am I doing wrong??
TIA . . . .
Dan
#!/usr/bin/env perl -w
package myTest;
use Carp;
our $AUTOLOAD;
use strict;
my %config = (
recursive => 0, # Bool
overwrite => 0, # Bool
);
sub new()
{
my $class = shift;
my $self = {
_permitted => \%config,
%config,
};
bless $self, $class;
return $self;
}
sub AUTOLOAD()
{
my $self = shift;
my $type = ref($self) || die;
my $name = $AUTOLOAD;
$name =~ s/.*://;
unless (exists $self->{_permitted}->{$name})
{
croak "Can't access $name field in object of class $type";
};
if (@_)
{
return $self->{$name} = shift;
}
else
{
return $self->{$name}
}
}
sub show_config()
{
my $self = shift;
print "Recursive: ".($self->recursive() ? "True" : "False")."\n";
print "Over-write: ".($self->overwrite() ? "True" : "False")."\n";
}
1;
package MAIN;
my $fred = myTest->new();
my $mary = myTest->new();
$mary->recursive(1);
$mary->show_config();