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

I hope the Title is informative enough. :)

I recently answered a simple post here. While I had no difficulty solving their riddle. I did find I had trouble getting Perl to better articulate any difficulties, should something go afoul.

While die (functions/die), and warn (functions/warn) get the job done to some degree. What if I'd like to help others with examples that provide more informative output from Perl? Sure use diagnostics can also be helpful. But to those new(ish) to Perl, the messages emitted can be somewhat cryptic. So I thought Carp might just be the answer, and started experimenting with it.

#!/usr/bin/perl -w # tester.cgi # dead simple script that hopefully won't work # as I want Perl to give me a nice informative # message, telling me why use strict; use Carp qw(cluck longmess shortmess); # example right out of perldoc my $long_message = longmess( "message from cluck() or confess()" ); # MYFILE does NOT exist my $file = "MYFILE"; # Perl, please inform me that the file MYFILE does not exist system ("cat $file" or $long_message());
Perl says
# ./tester.cgi syntax error at ./tester.cgi line 16, near "$long_message(" Execution of ./tester.cgi aborted due to compilation errors.
OK that wasn't what I had hoped for. Let's adjust it a bit
#!/usr/bin/perl -w use strict; use Carp qw(cluck longmess shortmess); my $long_message = longmess( "message from cluck() or confess()" ); my $file = "MYFILE"; system ("cat $file" or confess(),cluck(),carp());
Perl (or system command) says
cat: MYFILE: No such file or directory
Hmm. I knew MYFILE didn't exist, and cat told me so.

So is this the best way to get informative messages from Perl? This is a bit new to me, as I've always manufactured my error messages in the past 'cause I knew what the reason of failure (if any) would be.

Summary: how can I get the most informative, user friendly, error messages from Carp? Or is Carp even the best answer?

Thank you for all your consideration.

--Chris

Hey. I'm not completely useless. I can be used as a bad example.
  • Comment on How can I get Perl via Carp, to SCREAM -- more informative output via cluck(),confess(),longmess()?
  • Select or Download Code

Replies are listed 'Best First'.
Re: How can I get Perl via Carp, to SCREAM -- more informative output via cluck(),confess(),longmess()?
by choroba (Cardinal) on Dec 06, 2013 at 17:14 UTC
    Any string starting with "cat" is true. or respects parentheses, so the confess&Co are never called. You probably wanted to move or outside the system argument list? In such a case, note that system returns 0 upon success.
    (0 == system('cat', $file)) or confess();
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks for the reply, choroba

      I replaced my call to system, with the one you noted. which returned

      cat: MYFILE: No such file or directory Undefined subroutine &main::confess called at ./tester.cgi line 17.
      Thanks again for the reply.

      --Chris

      Hey. I'm not completely useless. I can be used as a bad example.
      

        Undefined subroutine &main::confess

        This is because you forgot to import the confess function. You're importing cluck, longmess and shortmess but nothing else from Carp:

        use Carp qw(cluck longmess shortmess);

        Really, longmess and shortmess are unlikely to be much use in end-user code. They might be helpful if you were writing an extension to Carp.

        The four functions that are actually useful are croak, confess, carp, and cluck, and the least useful of these is cluck (which happens to be the only one of them that you're importing).

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: How can I get Perl via Carp, to SCREAM -- more informative output via cluck(),confess(),longmess()?
by soonix (Chancellor) on Dec 07, 2013 at 10:25 UTC

    That's actually a bad example, because it's cat, not perl, detecting that the file doesn't exist. Perl can't add to this - not even the shell can't, because it doesn't know what cat will do with that argument string.

    That is the problem with all system and related functions (backticks, etc). Pure perl programs probably do profit from Carp and consorts.

      Good advice, soonix. Thank you.

      In my humble defense, I did mention cat threw the error. :)

      Thanks again, soonix.

      --Chris

      Hey. I'm not completely useless. I can be used as a bad example.
      
Re: How can I get Perl via Carp, to SCREAM -- more informative output via cluck(),confess(),longmess()?
by taint (Chaplain) on Dec 07, 2013 at 00:39 UTC