Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 204967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:30:52+00:00 2026-05-11T17:30:52+00:00

I am writing a class that is linked to an external resource. One of

  • 0

I am writing a class that is linked to an external resource. One of the methods is a delete method that destroys the external resource. No further method calls should be made on that object. I was thinking of setting a flag and die’ing inside of all of the methods if the flag is set, but is there a better, easier way? Something involving DESTROY maybe?

So far, I am really liking Axeman’s suggestion, but using AUTOLOAD because I am too lazy to recreate all of 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;
    #do whatever needs to be done to undelete resource
    return bless $self, $self->{orig_class};
}

sub AUTOLOAD {
    my $meth = (split /::/, $AUTOLOAD)[-1];
    croak "$meth is not a method for this object"
        unless $methods{$meth};
    carp "can't call $meth on object because it has been deleted";
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:30:52+00:00Added an answer on May 11, 2026 at 5:30 pm

    Is there a problem with simply considering the object in an invalid state. If the users hang on to it, isn’t that their problem?

    Here are some considerations:

    • Have you already decided whether it’s worth dying over?

    • Chances are that if you have a function that is encapsulated enough, you really don’t want to have the users parse through your code. For that purpose, you probably wouldn’t like to use what I call the Go-ahead-and-let-it-fail pattern. 'Can't call method "do_your_stuff" on an undefined value' probably won’t work as well for encapsulation purposes. Unless you tell them “Hey you deleted the object!

    Here are some suggestions:

    • You could rebless the object into a class whose only job is to indicate an invalid state. It has the same basic form, but all symbols in the table point to a sub that just says “Sorry can’t do it, I’ve been shut down (You shut me down, remember?).”

    • You could undef $_[0] in the delete. Then they get a nice 'Can't call method "read_from_thing" on an undefined value' from a line in their code–provided that they aren’t going through an elaborate decorating or delegation process. But as pointed out by chaos, this doesn’t clear up more than one reference (as I’ve adapted by example code below to show).


    Some proof of concept stuff:

    use feature 'say';
    
    package A;
    
    sub speak { say 'Meow!'; }
    
    sub done { undef $_[0]; }
    
    package B;
    
    sub new { return bless {}, shift; }
    
    sub speak { say 'Ruff!' }
    
    sub done { bless shift, 'A'; }
    
    package main;
    
    my $a = B->new();
    my $b = $a;
    
    $a->speak(); # Ruff!
    $b->speak(); # Ruff!
    $a->done();
    $a->speak(); # Meow!
    $b->speak(); # Meow! <- $b made the switch
    $a->done();
    $b->speak(); # Meow!
    $a->speak(); # Can't call method "speak" on an undefined value at - line 28
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a JavaSCript class that has a method that recursively calls itself. Scheduler.prototype.updateTimer
I'm writing a Vector3D class that calls a static method on a VectorMath class
I'm writing a factory class that should be able to return singleton instances of
am writing a class that has the same name for events and method, but
I am currently writing a stack class that uses linked lists. I feel like
I'm writing a class that filters a lot of values. What's the best way
I'm currently writing a class that implements the SeekableIterator interface and have run into
I'm busy writing a class that monitors the status of RAS connections. I need
I am writing a class that I know that needs a lock, because the
I'm writing a class that returns both a DataTable and a IEnumerable. I cannot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.