in reply to Die and Exit Codes

Maybe you could use $^S to see if you're in an eval. For example,

DieWithCode.pm:

use strict; use warnings; package DieWithCode; BEGIN { our @EXPORT = 'die_with_code'; require Exporter; *import = \&Exporter::import; } use Carp qw( carp croak ); sub die_with_code { my $code = shift(@_); if ($^S) { croak(@_); } else { carp(@_); exit($code); } } 1;

Test script 587042.pl:

use strict; use warnings; use DieWithCode qw( die_with_code ); $|=1; eval { die_with_code(4, "Foo"); }; print "Caught: $@" if $@; eval { die_with_code(5, "Bar\n"); }; print "Caught: $@" if $@; s/\\n/\n/g for @ARGV; die_with_code(@ARGV);

Test runs:

>perl 587042.pl 6 "Baz" Caught: Foo at 587042.pl line 9 Caught: Bar Baz at 587042.pl line 16 >echo %ERRORLEVEL% 6 >perl 587042.pl 7 "Boo\n" Caught: Foo at 587042.pl line 9 Caught: Bar Boo >echo %ERRORLEVEL% 7

Update: Made it into a module so carp and croak would print the right line number.