Re: Carp -- shortmess and longmess

From: Abigail (abigail_at_abigail.nl)
Date: 07/27/04

  • Next message: Stephen Patterson: "Asking questions in 'make test'"
    Date: 26 Jul 2004 22:52:28 GMT
    
    

    Haakon Riiser (hakonrk@fys.uio.no) wrote on MMMCMLXXXII September
    MCMXCIII in <URL:news:slrncg9ua0.emo.hakonrk@s.hn.org>:
    `' perldoc Carp says:
    `'
    `' carp - warn of errors (from perspective of caller)
    `' cluck - warn of errors with stack backtrace
    `' (not exported by default)
    `' croak - die of errors (from perspective of caller)
    `' confess - die of errors with stack backtrace
    `' shortmess - return the message that carp and croak produce
    `' longmess - return the message that cluck and confess produce
    `'
    `' but this is not how it appears to work. I created a test program
    `' based on the examples in the manual:
    `'
    `' --- begin carp.pl ---
    `' use Carp qw(croak cluck);
    `'
    `' sub foo {
    `' cluck "This is how we got here!";
    `' print Carp::shortmess("This will have caller's details added");
    `' print Carp::longmess("This will have stack backtrace added");
    `' croak "We're outta here!";
    `' }
    `'
    `' sub bar {
    `' foo();
    `' }
    `'
    `' bar();
    `' --- end carp.pl ---
    `'
    `' When I run this program, I get the following output:
    `'
    `' This is how we got here! at carp.pl line 4
    `' main::foo() called at carp.pl line 11
    `' main::bar() called at carp.pl line 14
    `' This will have caller's details added at carp.pl line 5
    `' main::foo() called at carp.pl line 11
    `' main::bar() called at carp.pl line 14
    `' This will have stack backtrace added at carp.pl line 11
    `' main::bar() called at carp.pl line 14
    `' We're outta here! at carp.pl line 7
    `' main::foo() called at carp.pl line 11
    `' main::bar() called at carp.pl line 14
    `'
    `' The message from cluck seems to be OK -- it prints the backtrace,
    `' as it should. The next two, shortmess and longmess, appear
    `' to do the opposite of what they should. shortmess prints the
    `' longest message, backtrace and everything, while longmess only
    `' prints the bottom of the stack. It should've been the other
    `' way around, right? croak also prints the full backtrace, which,
    `' if I understand the manual correctly, is incorrect.

    No, it's not incorrect. You have to read the manual
    carefully. longmess/cluck/confess return the stacktrace. Always.
    shortmess/carp/croak print the first entry in the stacktrace that isn't
    considered "safe". But when everything is "safe", it defaults to printing
    the entire stacktrace. The manual also list what is considered safe,
    and point 1 says any call into the same package is considered safe.

    A small change to your program shows the difference:

        #!/usr/bin/perl

        use strict;
        use warnings;
        no warnings qw /syntax/;

        use Carp qw(croak cluck);

        sub foo {
            cluck "This is how we got here!";
            print Carp::shortmess("This will have caller's details added");
            print Carp::longmess("This will have stack backtrace added");
            croak "We're outta here!";
        }

        package Alien;
        sub bar {
            main::foo();
        }

        package main;
        Alien::bar();
        __END__
        This is how we got here! at /tmp/f line 11
                main::foo() called at /tmp/f line 19
                Alien::bar() called at /tmp/f line 23
        This will have caller's details added at /tmp/f line 19
        This will have stack backtrace added at /tmp/f line 19
                Alien::bar() called at /tmp/f line 23
        We're outta here! at /tmp/f line 19

    Abigail

    -- 
    print v74.117.115.116.32, v97.110.111.116.104.101.114.32,
          v80.101.114.108.32, v72.97.99.107.101.114.10;
    

  • Next message: Stephen Patterson: "Asking questions in 'make test'"

    Relevant Pages