cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

In a few different cases now, I've been unable to tell if a particular message from Perl was a warning or an error. I've gone off on more than one wild goose chase because something I thought was an error was actually a warning. Most recent example here.

Other languages I've worked with distinguish between the two by listing them separately or prefixing the message with a label like "Warning" or "Error". Is there a reason why Perl does not do this? (I know you can suppress warnings with an interpreter switch but normally I want them.)

Steve

Replies are listed 'Best First'.
Re: Warnings vs. Errors
by Joost (Canon) on Aug 13, 2008 at 17:21 UTC
    Errors forcibly exit the program with a non-zero exit code (unless caught - if possible - using eval {}). Warnings don't (and can't be caught with eval {} either).

    See also the difference between die and warn.

    update: to check for a non-zero exit code, you can do something like this in a bash/sh type shell:

    perl some_program.pl || echo "That was an error"
    update 2 please don't double post. especially not an hour after the first post.

      The double post was an accident that happened because I refreshed the page that comes up after you post a post, thinking that would give me a look at any replies that had come up. Instead it made a dupe. (Why is it designed that way?)

      As soon as I realized what happened I asked on CB for someone to delete it and was told it couldn't be done.

Re: Warnings vs. Errors
by mr_mischief (Monsignor) on Aug 13, 2008 at 17:19 UTC
    Well, a few things come to mind immediately. One is that warnings are lexically scoped these days, so it should be easier to test that theory. Another is the diagnostics pragma (or splain), which explains what went wrong when you get an error or warning. The line use diagnostics -verbose; right after your other pragmas will tell you more than you wanted to know.
Re: Warnings vs. Errors
by ikegami (Patriarch) on Aug 13, 2008 at 17:59 UTC

    Why? It sounds like you want to ignore warnings without investigating or fixing them. If that's case, why don't you just turn them off? If that's not the case, do you really need Perl to tell you whether or not the program ended afterwards?

    You'd be better off checking the error code anyway. Doesn't the IDE you say you're using display the process's error code?

      Hmm, that's a good point. I suppose I want to get the warnings till I can determine if they can be safely ignored or not.

      In another case, I had a functioning script running on a cron job that never had problems. I got a message from cron and someone asked me if it was an error or warning and I didn't know.

Re: Warnings vs. Errors (DIY)
by tye (Sage) on Aug 13, 2008 at 19:14 UTC
    $SIG{__WARN__} = sub { warn "WARNING (please ignore): @_" };

    - tye        

Re: Warnings vs. Errors
by Perlbotics (Archbishop) on Aug 13, 2008 at 20:34 UTC
    Even $SIG{__DIE__} exists. For finer granularity (fatal, error, warning, info, debug, trace) you might be interested in Log::Log4perl?
Re: Warnings vs. Errors
by toolic (Bishop) on Aug 14, 2008 at 01:09 UTC
    Somewhat related... it is also possible to force warnings to behave like errors, using Fatal Warnings:
    use warnings FATAL=>'all';