Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Carping in DESTROY.

by eff_i_g (Curate)
on Oct 24, 2006 at 16:21 UTC ( #580314=perlquestion: print w/ replies, xml ) Need Help??
eff_i_g has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm calling Storable's store within a DESTROY method, and I'm not getting an error for a read-only file. I was (still am) very puzzled, but I eventually received the error by changing "DESTROY" to "end" and explicitly calling $object->end();.

Why am I not getting the error in DESTROY, and how can I handle this properly? A tiny code snippet is below, which works properly when DATA_FILE is writeable:
sub DESTROY { my $self = shift; store $self, DATA_FILE; }
I appreciate the help as I venture further into OOP :)

Comment on Carping in DESTROY.
Select or Download Code
Re: Carping in DESTROY.
by davido (Archbishop) on Oct 24, 2006 at 17:18 UTC

    Storable's documentation states that store() returns undef on failure, and that failure usually indicates an IO error. Thus, you should be checking its return value just as you would check the return value of other IO calls.

    store $self, DATA_FILE or die "Couldn't store object.\n";

    Dave

      davido,

      I tried die and eval; neither of them show an error. I'll prepare some example code...

      I don't know where this is documented but die() in DESTROY isn't thrown on my 5.8.7.

      sub A::DESTROY { die 'ok' } my $obj = bless [], 'A'; $obj = ''; # no exception occurs print "$@\n"; # " (in cleanup) ok at - line 1."

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        It does get thrown — anything after the die won't get executed — but it's intercepted by DESTROY's caller.

        my $var = 1; sub A::DESTROY { $var = 2; die 'ok'; $var = 3; } my $obj = bless [], 'A'; $obj = ''; # no exception occurs print "$var\n"; # 2 print "$@\n"; # " (in cleanup) ok at - line 1."

        This behaviour isn't new. Tested with 5.6.1

Re: Carping in DESTROY.
by madbombX (Hermit) on Oct 24, 2006 at 17:36 UTC
    You should also be storing the object and not a reference to the object:
    store \$self, DATA_FILE or warn "store error (%a): $!\n";

    You should also always be in the habit of checking your results (hence the or warn "blah";).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://580314]
Approved by friedo
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (19)
As of 2014-07-28 19:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (207 votes), past polls