[Detroit-pm] Trying to get a handle on exceptions

Scott Webster Wood treii28 at yahoo.com
Thu May 22 12:23:49 PDT 2008


OK, so you got this nifty little config file reader that reads some company's legacy config file format much better than their old scripted perl stuff did.  But when you made it, you were smart enough to put some error conditions that would either 'die' or 'croak' with Carp when things occurred that would prevent it from doing anything further (file not found, file not readable, etc) as well as the occasional carp that complains about things it finds that aren't quite kosher.

So you did it with a typical perl oop:

use readcfg;
my $cfg = new readcfg(-cfgfile => 'somefile.cfg');

....


but let's just say that I don't want the program calling the config reader to die when the read dies, but just to get an error condition back instead?  I've been going over many websites but can't quite get my brain wrapped around the best way to handle this.  I know if you call some of the built in stuff, they put the error stuff in errno in a nice nifty fashion that you can look up with $!.  The platform I am working on isn't really going to allow me to add-in stuff like Error.pm, and I went to a bunch of sites using variations of try/catch or $SIG{__DIE__} but trying the examples they showed, I couldn't seem to get the same results that they suggested I should.

Looking through my script it looks like there are only three fatal conditions.  The object has a method 'init' that starts the actual read process.  It requires a single parameter, a filename or filepath and does a single test '-f' to see if the file exists failing if it doesn't.  It will then pass things off to a private method that actually parses the file but that begins by passing off to another simple method that just 'reads' a file.  The other two fatals are in there, one being if no filename/filepath is passed to it at all (I also predict calling that method elsewhere so wanted to make sure again that there was a filename passed) and then the other croak is tied to the actual 'open' IO function itself.
the new method also includes the ability to pass the -cfgfile => 'somefile' type context that will then run $self->init('somefile'), thus the syntax I gave above.

So the big two that I am worried about are pretty much that 'init' gets a value that points to real file and that the file can be opened.  my config reader dies in either of these two cases, but I don't want the program using the config reader to die when this happens.  I know I can go in and pass error conditions back from the init, etc. but since I want to call init from new, it makes that difficult since 'new' is passing back the object itself.

SW



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/detroit-pm/attachments/20080522/121d2619/attachment.html 


More information about the Detroit-pm mailing list