In my experiment, this solution only works when the caller comes from a different file. Adding Try::Tiny
to the @CARP_NOT
variable for the package fixes in this case:
#first file
use MyPackage;
MyPackage::foo();
#second file
package MyPackage;
use Carp qw(carp cluck);
our @CARP_NOT = qw(Try::Tiny);
use Try::Tiny;
sub foo {
carp 'bar1';
cluck 'bar2';
try{
carp 'bar3'
}catch {
cluck $_;
}
}
1;
It prints the following:
bar1 at myFile.t line 2.
bar2 at MyPackage.pm line 8.
MyPackage::foo() called at myFile.t line 2
bar3 at myFile.t line 2.