Hi everyone!

I'm trying to use Error in my command line utility and had some troubles doing so. For example:
#!/usr/bin/perl -w # filename: test.pl Use Error qe(:try); try { &func1(); print "still in try block\n"; } catch Error::Simple with { my $err = shift; print "Error: ", $err->{'-text'}, "\n"; print "had some problem\n"; } otherwise { print "in otherwise block\n"; } sub func1 { print "in func1\n"; throw Error::Simple('throwing simple error'); }
Several things:
1. The above example doesn't work, I found out later that func1() should be defined BEFORE the try block. Why is that?

2. After I moved the definition of func1() before the try block (now it's working), I still didn't understand how can I return to the try block execution after the catch block finishes, from CPAN:

catch CLASS with BLOCK
BLOCK will be passed two arguments. The first will be the error being thrown. The second is a reference to a scalar variable. If this variable is set by the catch block then, on return from the catch block, try will continue processing as if the catch block was never found.Can anyone help here?

3. If I add at the end of the file a print print "End of program\n";, I get the following error (the program exits after printing the line):

Can't use sting ("1") as a HASHREF while "strict refs" in use at /usr/lib/perl5/site_perl/5.8.0/Error.pm line 456

If I put two print lines at the end, only the first one is printed. As I said I want to put the try/catch in the main loop of my CLI to catch exeptions of command execution, so this problem realy gets me stucked, anyone for this one?

Hotshot

In reply to Error module by hotshot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.