in reply to Is it legal to create subs in a module?

Methods are stored at the class level, not at the object level. In Perl terms, when you create a method you are installing a subroutine in the symbol table for your package. Removing an object doesn't do anything to the package's symbol table.

A better way to do this is using AUTOLOAD like this:

sub AUTOLOAD { my ($pkg, $attr) = $AUTOLOAD =~ /(.*)::(.*)/; *{$attr} = sub { return $_[0]->{$attr} }; return $_[0]->{$attr}; }

Although that still doesn't solve your main problem.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you don't talk about Perl club."

Replies are listed 'Best First'.
Re: Re: Is it legal to create subs in a module?
by runrig (Abbot) on Oct 11, 2001 at 20:00 UTC
    Doesn't both your's and lestrrat's answer(s) need to ignore the DESTROY method? Like:
    return if $AUTOLOAD =~ /::DESTROY$/;

      Good point. I'd probably get round that by defining a dummy DESTROY method that does nothing.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you don't talk about Perl club."

Re: Re: Is it legal to create subs in a module?
by Fletch (Bishop) on Oct 11, 2001 at 20:27 UTC

    Of course you could be evil and use a blessed coderef as your representation. That coderef could dispatch methods differently for each instance (the instance information being kept in a lexical visible when the closure is created). But you probably can only get away with that if you're Damian (c.f. Object Oriented Perl, Chapter 11, p297-300).