|Perl: the Markov chain saw|
Re^3: when is destroy function calledby Athanasius (Canon)
|on Mar 02, 2013 at 08:19 UTC||Need Help??|
Sorry, my bad. I added use warnings to my copy of ggoebel’s script; forgot that it wasn’t in the original; and then compounded the confusion by referring to the warning which results as an “error’. The warning in question is:
which comes from the inclusion of undef($obj) in the arguments to the print statement.
The evals are there to allow the print statement to complete in spite of the error (exception) which results from the attempt to call a method on an undefined object. That error is:
which in the original example was captured by the eval and printed by the statement print $@ if $@;.
I agree that your example “seems to prove” my conclusion; but I wanted to be sure. Hence my provision of an additional resource which is altered within the DESTROY method. Adapting your example:
which shows that $num is incremented only after the print statement completes, and not at the point where the reference count of $dog falls to zero. (Note that the “1” in the output comes from the say statement in Dog::bark, which returns true on success.)
Hope that helps,