Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re5: Error module

by hotshot (Prior)
on Aug 19, 2003 at 05:40 UTC ( [id://284799]=note: print w/replies, xml ) Need Help??


in reply to Re5: Error module
in thread Error module

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

Replies are listed 'Best First'.
Re7: Error module
by dragonchild (Archbishop) on Aug 19, 2003 at 12:47 UTC
    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
        Yeah, I see a number of things wrong.
        1. Don't code as root. In fact, don't EVER do anything as root unless you absolutely have to. It's dangerous and stupid.
        2. Don't code in /usr/local/bin. In fact, don't code in a bin directory. Ever. Code in some source directory, preferably in a directory under your home directory. I have a directory called 'perl' that I work in.

        Given that you aren't going to fix the above issues before testing this again, I would add use lib qw(/usr/local/bin); above the use MyErrors::SomeError; line. Normally, you run the perl script from the directory it's in. This allows modules that are in that directory (or subdirectories from it) to be auto-included. (This is really nice once you have a proper development environment set up.)

        ------
        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.

Re^7: Error module
by particle (Vicar) on Aug 19, 2003 at 13:17 UTC

    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*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-26 04:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found