fireartist has asked for the wisdom of the Perl Monks concerning the following question:

I have a file "Config/File.pm"

package Config::File; use 5.000; use strict; use warnings; use Carp; require Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD); @ISA = qw(Exporter); $VERSION = '0.01'; sub new { my $class = shift; my %arg = @_; my $self = {}; bless( $self, $class ); croak($class.'->new() requires file name') unless $arg{file}; $self->{'conf'} = do($arg{file}); return $self; } sub AUTOLOAD { my $self = shift; my $method = $AUTOLOAD; $method =~ s/.*://; unless (defined $self->{'conf'}->{$method}) { croak($method.' not defined in '.$self); } if (ref $self->{'conf'}->{$method} eq 'HASH') { return %{ $self->{'conf'}->{$method} }; } elsif (ref $self->{'conf'}->{$method} eq 'ARRAY') { return @{ $self->{'conf'}->{$method} }; } else { return $self->{'conf'}->{$method}; } } 1;

A file "conf.pl"

{ en => 'one', de => [qw/ein zwei/], ch => { 1 => 'yi', 2 => 'er'}, }

And a file "test.pl"

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Config::File; my $c = Config::File->new(file => 'conf.pl'); my $en = $c->en; my @de = $c->de; my %ch = $c->ch; print Dumper(\$en, \@de, \%ch);

When I run "test.pl" I get the output
$VAR1 = \'one'; $VAR2 = [ 'ein', 'zwei' ]; $VAR3 = { '1' => 'yi', '2' => 'er' }; (in cleanup) DESTROY not defined in Config::File=HASH(0x224f74) at + test.pl line 0

I don't understand the error about DESTROY not being defined, could someone explain what's causing it?

janitored by ybiC: Balanced <readmore> tags around long code block

Replies are listed 'Best First'.
Re: DESTROY not defined error
by broquaint (Abbot) on Jan 23, 2004 at 10:49 UTC
    This is because DESTROY is implicitly called upon the destruction of the object, and because you've defined an AUTOLOAD method, that's trying to deal with it but failing since DESTROY hasn't been defined. If you add a dummy method the error will go away e.g
    sub DESTROY { }
    See. perlobj for more info.
    HTH

    _________
    broquaint

      Right

      It's a general rule that when you do OO-Perl and you use an AUTOLOAD method you should always explicitly define a DESTROY method as well (unless AUTOLOAD takes care of DESTROY, too); even an empty one.

      Another trick that you could use to speed up things is to have AUTOLOAD install a method in the package namespace each time it is called. This was a trick that TheDamian explained in his book "Object Oriented Perl". Take a look at this node by Ovid to see how (or buy the book ;)

      Ciao!
      --bronto


      The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
      --John M. Dlugosz
      Note that if it doesn't find a DESTROY method for an object, perl doesn't mind; perl only calls DESTROY if it appears to exist. And if you have an AUTOLOAD method, perl (intentionally) will find that when looking for a DESTROY.

      (For what it's worth, the import and unimport methods implicitly called by use and no don't work this way; if they don't exist but AUTOLOAD does, AUTOLOAD isn't called.)