In addition to responses above me (which all point you in the direction you probably want to go in), I'd like to tell you something interesting about the behaviour of die I recently discovered.

This behaviour is documented in the description of die. Allow me to quote.

die LIST
... Inside an eval(), the error message is stuffed into $@ and the eval is terminated with the undefined value. This makes die the way to raise an exception.
...
If the last element of LIST does not end in a newline, the current script line number and input line number
...
(paraphrased: "input line number" means whatever the current value of $/ is. Usually just \n, though.)

Good. Now the underlined part is important there. It implies that if you include $/ at the end of your die message, you can prevent the at line X of Y to be added to the die message. That raises an interesting use of die. Let's see.

my $codeblock = "Important and unimportant stuff"; eval { doImportantStuff; #if anything goes wrong, it goes `die "invalid important stuff\n"; +` #This should be logged doUnimportantStuff; # `die "invalid unimportant stuff\n";` # Should not be logged. moreImportantStuff; #`die "more invalid important stuff\n";` # Should be logged }; log_error("$@", $codeblock) if $@; sub log_error { return unless DEBUG; my $error = shift @_; my $place = shift @_; my @logthese = ( "invalid important stuff\n", "more invalid important stuff\n", ); for my $check (@logthese) { if ($error eq "$check") { write_to_log("Error '$error' at $place"); last; # found! No need to continue searching } } }

Code is only intended to illustrate a point and therefore untested. Use at own discretion.

Update: spotted a stupidity in the code, fixed that.


In reply to Re: novice 'die' help requested by muba
in thread novice 'die' help requested by nmerriweather

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.