I am writing a class associated with an external resource. One method is the delete method, which destroys an external resource. No further method calls should be made on this object. I was thinking about setting a flag and dying in all methods if the flag is set, but is there a better, easier way? Perhaps something related to DESTROY?
So far I really like the Axeman suggestion, but using AUTOLOAD because I'm too lazy to recreate all the methods:
#!/usr/bin/perl use strict; use warnings; my $er = ExternalResource->new; $er->meth1; $er->meth2; $er->delete; $er->meth1; $er->meth2; $er->undelete; $er->meth1; $er->meth2; $er->delete; $er->meth1; $er->meth2; $er->meth3; package ExternalResource; use strict; use warnings; sub new { my $class = shift; return bless {}, $class; } sub meth1 { my $self = shift; print "in meth1\n"; } sub meth2 { my $self = shift; print "in meth2\n"; } sub delete { my $self = shift; $self->{orig_class} = ref $self; return bless $self, "ExternalResource::Dead"; } package ExternalResource::Dead; use strict; use Carp; our $AUTOLOAD; BEGIN { our %methods = map { $_ => 1 } qw/meth1 meth2 delete new/; } our %methods; sub undelete { my $self = shift;
oop perl design-decisions
Chas. Owens
source share