in reply to Re: Re: Re: Re: Error module
in thread Error module

All OO in Perl is done through packages. As all the errors in the Error paradigm are objects, you're going to have to use packages. *shrugs*

Personally, most of my packages look like:

package MyErrors::SomeError; use Error::Simple; @ISA = qw(Error::Simple); 1;

(Yes, I know there's no strict, warnings, or whatever. With something this simple, I tend to skip those things.)

Then, when you want to use it, you can just catch that error.

The really neat thing about doing this is that you can create classes of errors. Let's say you have 10 errors that can arise from a database action. Instead of having to handle each one, you can have a base class of MyError::Database and have all your DB errors use it as the base class. MyError::Database would inherit from Error::Simple, thus allowing you to handle all your errors in one place, if you wanted.

That kind of hierarchy is one of the huge benefits of OO, allowing you to specify where in the hierarchy you want to work in.

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re5: Error module
by hotshot (Prior) on Aug 19, 2003 at 05:40 UTC
    I'm not too familiar with OO programing in perl, so a little question that may seem foolish. In the example you gave:
    package MyErrors::SomeError; use Error::Simple; @ISA = qw(Error::Simple); 1;
    Do I need to put these lines in a .pl file and include it (use or package?)in my program in order to be able to catch these errors, or is there another way?

    Hotshot
      Please help yourself and read up on Perl OO. There are very good books on the matter, one written by our very own Damian Conway. There is also a good section in the Llama book, by our very own Randal Schwartz.

      The minimum you'd need to use what I gave you would be to put those lines in a file called SomeError.pm which would be located in a directory called MyErrors. The directory MyErrors should be in the same directory as your Perl script. (You can actually put it anywhere, but it's easiest to just put it there.)

      In your Perl script, you put use MyErrors::SomeError; right after the use Error qw(:try); line.

      Good luck! If you try this and end up with errors, please post your script and what errors you're getting. That way, we can best help you.

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        First of all, thanks for your patience. Secondly, I still can't seem to get this going, I acted exactly as you said, created /usr/local/bin/MyErrors/SomeError.pm with the contents:
        package MyErrors::SomeError; use Error::Simple; @ISA = qw(Error::Simple); 1;
        Then I'v created /usr/local/bin/test.pl with the following contents:
        #!/usr/bin/perl -w use Error qw(:try); use MyErrors::SomeError; try { throw MyErrors::SomeError('throwing SomeError exception'); } catch MyErrors::SomeError with { my $err = shift; print "caught it: $err->{'-text'}\n"; };
        When I run test.pl I get the following error:

        Can't locate MyErrors/SomeError.pm in @INC (@INC contains: /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0) at /usr/local/bin/test.pl line 4. BEGIN failed--compilation aborted at /usr/local/bin/test.pl line 4.

        It seems that it can't find SomeError.pm, sorry again for the troubling but do you see something wrong here?

        Hotshot

      it seems from your reply that it's not just oo perl that you're unfamiliar with, but modules in general. take a look at perlmod and perlmodlib for module information (including some oo info.) to study up on oo perl, perltoot and perltooc are good tutorials.

      ~Particle *accelerates*