in reply to novice 'die' help requested
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: novice 'die' help requested
by friedo (Prior) on Jan 11, 2007 at 22:18 UTC | |
by muba (Priest) on Jan 11, 2007 at 23:46 UTC | |
|
Re^2: novice 'die' help requested
by nmerriweather (Friar) on Jan 13, 2007 at 01:03 UTC |