Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Is there a problem with simply considering the object in an invalid state. If the users hang on to it, isn't that <em>their</em> problem? </p> <p>Here are some considerations: </p> <ul> <li><p>Have you already decided whether it's worth dying over? </p></li> <li><p>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. <code>'Can't call method "do_your_stuff" on an undefined value'</code> probably won't work as well for encapsulation purposes. Unless you tell them "Hey you deleted the object!</p></li> </ul> <p>Here are some suggestions: </p> <ul> <li><p>You could <em>rebless</em> 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?)."</p></li> <li><p>You could undef <code>$_[0]</code> in the delete. Then they get a nice <code>'Can't call method "read_from_thing" on an undefined value'</code> from a line in <em>their</em> 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).</p></li> </ul> <hr/> <p>Some proof of concept stuff: </p> <pre><code>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-&gt;new(); my $b = $a; $a-&gt;speak(); # Ruff! $b-&gt;speak(); # Ruff! $a-&gt;done(); $a-&gt;speak(); # Meow! $b-&gt;speak(); # Meow! &lt;- $b made the switch $a-&gt;done(); $b-&gt;speak(); # Meow! $a-&gt;speak(); # Can't call method "speak" on an undefined value at - line 28 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload