Subject: | Exception not available in $_ within a given/when construct |
A catch{} block inside a when{} block does not see the thrown exception
in $_. If the catch block code examines $_, it will find the given()
expression. Apparently, given/when masks $_ somehow.
Workaround: The exception can be found in $::_, within a given/when
construct or not. Alternatively, the exception can be found by using
the passed argument to the catch{} code block.
The attached file demonstrates the problem, and the $::_ workaround.
Subject: | catch_problem.pl |
use feature 'switch', 'say';
use Try::Tiny;
try
{
die 'Oh hell';
}
catch
{
say qq{catch top-level', \$_ is "$_"};
};
given ('condition')
{
when ('condition')
{
try
{
die 'WTF';
}
catch
{
say qq{catch in 'given', \$_ is "$_"};
say qq{catch in 'given', \$::_ is "$::_"};
say qq{catch in 'given', arg is "$_[0]"};
};
}
}
for ('condition')
{
try
{
die 'WTF';
}
catch
{
say qq{catch in 'for', \$_ is "$_"};
say qq{catch in 'for', \$::_ is "$::_"};
};
}