Please note that you're loading the object from a file with the name of the dereferenced $sref_file, but you're storing it in './object.dat,' even though you pass $sref_file to that as well- that's simple to fix :)
I'm also not a big fan of Exception; rather, I like to use Error. I just love being able to throw different types of errors with about two seconds of code as well as the utter extensibility that Error provides:
package SomeError; @ISA = qw(Error::Simple);
Then you can throw an error of type 'SomeError', and you can create even more complicated hierarchies of errors so easily with Error.pm (just have another, more specific subset of SomeError inherit from it.) It's a perfect use of Universal::isa().
Error's errors also propogate down, so if you throw an exception on an upper sub, you can catch it later. This allows you to not have to return the error if there is one, and instead follow the standard rule of 0 or undefined on failure, and true of success (you could still do this, but Error.pm makes it really intuitive to me.) Furthermore, you wouldn't have to comment out the thrown errors, because if they weren't asking to be caught somewhere, they'd never really be used (this is my knowledge of Error.pm- just use record(), and never catch the error. However, this is possible using either error module again, probably)...
Also, afaik- \%{ $self } == $self, so
Storable::store ( \%{ $self }, './object.dat' ); could be Storable::store ($self,'./object.dat' );.
Instead of storable, I'm also becomming a fan of YAML, if you haven't tried it- try it! It's pretty cool, and you can actually read the files it generates (I'm not sure if it'd be good for saving a reference to an object, because I've never tried that using it, but I expect it would do fine...) | [reply] [d/l] [select] |
sub store_object {
my ( $self, $sref_file ) = @_;
# Caller Check
my $return = $self->_caller_check();
if ( $return ) {
print STDERR $$return . "\n";
return $return;
}
Storable::store ( \%{ $self }, $$sref_file );
return undef;
}
Peace Out,
DeadPoet
| [reply] [d/l] [select] |
Why write \%{$self}? It's a waste of both space and execution time for your code (perl may optimize it, but it's still a waste of space...) Instead, use $self, which is the exact same thing, and makes it obvious that you're putting a blessed object into the file (it took me a while to comprehend that \%{$self} somehow kept the object blessed and returned $self, itself-- this is completely countra-my previous knowledge of perl. Could somebody explain why that works?)
| [reply] |