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

If its message parameter ends in a new line, die WILL NOT add the line number to the end of the string. Croak is suppose to be similar to die. However, If its message parameter ends in a new line, croak WILL add the line number to the end of the string. Why is that? Is this a bug or did they mean to do that?

The following program illustrates the difference.

#!/usr/bin/perl -w use Carp; eval { eval { die "Pig\n"; }; if ($@) { print $@."\n"; die $@; } }; print $@ if $@; eval { eval { croak "Pig\n"; }; if ($@) { print $@."\n"; croak $@; } }; print $@ if $@;
On my machine, it returns ...
Pig Pig Pig eval {...} called at ./test7.pl line 13 eval {...} called at ./test7.pl line 12 Pig eval {...} called at ./test7.pl line 13 eval {...} called at ./test7.pl line 12 eval {...} called at ./test7.pl line 12

Replies are listed 'Best First'.
Re: croak not like die.
by brian_d_foy (Abbot) on Mar 16, 2005 at 19:11 UTC

    Your sample program is a bit convoluted to be useful, so I wrote my own:

    #!/usr/bin/perl use Carp; eval { croak "With a newline\n" }; print $@; eval { croak "No newline" }; print $@;

    Here's my output on perl v5.8.4 built for darwin-2level. With a newline, there is nothing after the message "With a newline", although croak still tell me where it happened. After the message "No newline", I get the file name and line number, and croak still tells me a little bit about where this happened.

    With a newline eval {...} called at croak.pl line 5 No newline at croak.pl line 7 eval {...} called at croak.pl line 7

    This is the output I expect: If I'm using croak(), I want that information about the caller. That's a separate issue from tacking on the filename and line number though.

    --
    brian d foy <bdfoy@cpan.org>
Re: croak not like die.
by RazorbladeBidet (Friar) on Mar 16, 2005 at 18:57 UTC
    This is what I got:
    perl -MCarp -le 'croak "Died here"' Died here at -e line 1
    vs.
    perl -MCarp -le 'croak "Died here\n"' Died here
    from the perldoc:
    The Carp routines are useful in your own modules because they act like die() or warn(), but with a message which is more likely to be useful to a user of your module.
    soooo.... it's not the same, but like.
    --------------
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
      I get
      Pig Pig Pig at C:\t.pl line 14 eval {...} called at C:\t.pl line 13 eval {...} called at C:\t.pl line 12 Pig at C:\t.pl line 14 eval {...} called at C:\t.pl line 13 eval {...} called at C:\t.pl line 12 at C:\t.pl line 16 eval {...} called at C:\t.pl line 12
      (ActivePerl 5.8.6)


      holli, /regexed monk/
      I get
      > perl -MCarp -le 'croak "Died here"' Died here at -e line 1 > perl -MCarp -le 'croak "Died here\n"' Died here at -e line 1

      Which is not the same as your ouput, but I would consider mine better, since the reason to use croak is to give the file/line message in the first place.

      This is perl, v5.8.6 built for i686-linux.
      (with -DDEBUGGING by the way, though I doubt that matters.)