Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: General Class Creation Using Persistent Object, Method Privacy Enforcement and Exceptions

by Revelation (Deacon)
on Jun 30, 2003 at 04:42 UTC ( [id://270077]=note: print w/replies, xml ) Need Help??


in reply to General Class Creation Using Persistent Object, Method Privacy Enforcement and Exceptions

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...)
  • Comment on Re: General Class Creation Using Persistent Object, Method Privacy Enforcement and Exceptions
  • Select or Download Code

Replies are listed 'Best First'.
Re: Re: General Class Creation Using Persistent Object, Method Privacy Enforcement and Exceptions
by DeadPoet (Scribe) on Jun 30, 2003 at 14:56 UTC

    Thanks, I missed the fact that I had hard coded the store file name but had passed the reference to the file name. The sub store_object {} should read as follows:

    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

      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?)
        I agree. \%{$self} takes about five minutes to type. $self takes 1.618033989 seconds to type(I timed it.) To use an analogy, 1.618033989 is a lot easier than writing {sqrt(5) + 1}/2. And phi is easier still. As the author of "Walden Pond" asserted concisely, "Simplify, simplify." -100104
        After evaluating the difference between \%{ $self } and $self, I see that you are correct. I see that I am making perl perform a dereference to the same memory location. I will make corrections in my code. Thanks for the input.

        Peace Out,

        DeadPoet

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://270077]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found