in reply to Re^7: eval to replace die?
in thread eval to replace die?
Come on, this is just silly. Yes isa() will use string compares, but to say it's just a string compare is like saying a marathon is "just moving your feet". Just one example is that isa() knows how to look up the inheritance tree and tell me if one of my ancestors also meets the criteria.
Here's an example from a recent project of mine where structured exception objects helps out a lot. I have some code that runs remote commands on different servers. This code could be called from lots of different places and depending on how it's called the error handling is different. I could just die "Couldn't run $cmd on server $server", but then I have to parse that string with regular expressions to get the $cmd and $server values, every place it could be used (the same regular expression in lots of different places, not very DRY). And if I ever decide to change that error message I have to then track down all the places that regular expression lives (which is much, much harder than searching for a simple string in piles of code) and change them. It gets even worse if this module is used in places that I don't control (I'm flattered when someone uses my CPAN modules and don't want to make their job any harder than it has to be).
But instead I can use an Exception::Class object and do something like Project::FailedRemoteCommand->throw(error => "Couldn't run $cmd on server $server", command => $cmd, server => $server). Now the code that uses this doesn't have to care about my actual error message, just the type of error. Not only is searching for "FailedRemoteCommand" in a code base easier than searching for regex, it's also more future proof. Modules that use my lib don't have to care about the exact text of my error message either. They can get the $e->server or $e->command easily. And if they don't care about the structured exception, it still works just like a string to them.
Yes they might have problems if I ever decide to restructure my exception hierarchy (which is much rarer than changing an error message) but even that can be handled by having some special isa() magic to make the new classes still look like the old to old code. Try doing that when you're just using strings.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^9: eval to replace die?
by BrowserUk (Patriarch) on Oct 05, 2010 at 02:04 UTC | |
by mpeters (Chaplain) on Oct 06, 2010 at 19:45 UTC | |
by BrowserUk (Patriarch) on Oct 06, 2010 at 21:18 UTC | |
by mithaldu (Monk) on Oct 08, 2010 at 12:32 UTC | |
by BrowserUk (Patriarch) on Oct 08, 2010 at 15:31 UTC | |
| |
by phaylon (Curate) on Oct 05, 2010 at 13:21 UTC | |
by BrowserUk (Patriarch) on Oct 05, 2010 at 13:35 UTC | |
by phaylon (Curate) on Oct 05, 2010 at 14:01 UTC | |
by BrowserUk (Patriarch) on Oct 05, 2010 at 14:16 UTC | |
|