Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Yet more Try::Tiny problelms

by dd-b (Monk)
on May 18, 2013 at 21:39 UTC ( #1034160=perlquestion: print w/ replies, xml ) Need Help??
dd-b has asked for the wisdom of the Perl Monks concerning the following question:

This may be an interaction with Log::Log4perl

$logger->trace("Point 9.85"); # create an API notice try {$self->publish_notice( $subject->upload->mapping->map_id, $csv_row->row_id, q(InvalidValue), $csv_row->grain, q(Domain::Products), $item_name // '' );} catch { $logger->trace("point 9.9"); $logger->logdie("Notice failed: ",Dumper($_)); $logger->trace("point 9.92"); die ("Notice failed ".Dumper($_)); }; # stuff something into the log $logger->trace("Point 9.9");

is producing the following log output:

20130518 16:19:53.264 TRACE 29309 PreparePayment.pm(175) _run: Point 9.85 
20130518 16:19:53.268 TRACE 29309 PreparePayment.pm(184) __ANON__: point 9.9 
20130518 16:19:53.269 FATAL 29309 PreparePayment.pm(185) __ANON__: Notice failed: $VAR1 = 'sale-item'; 
20130518 16:19:53.272 ERROR 29309 PreparePayment.pm(78) __ANON__: run(OT::DB::Result::Csv=HASH(0xa47dd00)): $VAR1 = '';

That last log line, from PreparePayment line 78, comes from the $logger call in this:

try { $self->_run($row_id, $subject) } catch { $logger->error("run($row_id): ".Dumper($_)) };

So, you see what's happening there? $self->publish_notice() is failing (in ways that are also impossible) and putting us into the catch block. We log that we're at point 9.9, and we log the "Notice failed" message. That same message should be thrown as an exception by logdie(), and we don't get to point 9.92 so it looks like the die probably happened. However, up around line 78 where we catch that exception, we get an empty string.

It may be relevant that $self->publish_notice is from a Moose::Role, and that $logger calls in there (it fetches its own logger, and checks it's non-null) don't result in log output.

Anybody have any thoughts? I've gone through our previous discussions of Try::Tiny and I don't see any of the problematic things happening here.

Comment on Yet more Try::Tiny problelms
Select or Download Code
Re: Yet more Try::Tiny problelms
by Anonymous Monk on May 19, 2013 at 03:23 UTC

    Anybody have any thoughts? I've gone through our previous discussions of Try::Tiny and I don't see any of the problematic things happening here.

    :) make it reproducible, trim trim trim until you have solved problem, or have short self contained example of the unwanted behaviour (a test case)

      And, bingo!

      #! /usr/bin/perl use feature qw (switch); use Try::Tiny; my $uv; given (17) { when (3) { print "three\n"; }; when (17) { try { print "In try block\n"; die( "Substitute undefined variable $uv\n"); print "Still in try block\n"; } catch { print "We caught $_\n"; }; } }

      Output is:

      $ ./t4.pl 
      In try block
      We caught 17
      

      It was indeed an interaction between Try::Tiny and the switch feature. Might not happen in later versions of Perl, according to what I've read, but we're running 5.10, where it does.

        :) happens in v5.16.1 also, but if you search Try::Tiny for $_ this caveat is described but real solution is not

        I found this thread I participated in Lexical $_ in given/when vs. BLOCK arguments which had the real solution, because I ran into similar nonsense before :)

        #!/usr/bin/perl -- use feature qw (switch); use Try::Tiny; my $uv; given (17) { when (3) { print "three\n"; }; when (17) { #~ local $_; # grr Can't localize lexical variable $_ #~ my $_; ## doesn't help, Try::Tiny code is using global $_ #~ local *_; ## doesn't help , here $_ refers to lexical $_ our $_; # THIS HELPS try { print "In try block\n"; die( "Substitute undefined variable $uv\n"); print "Still in try block\n"; } catch { print "We caught global \$@ = $@\n"; print "We caught lexical \$_ = $_\n"; print "We caught global \$::_ = $::_\n"; print "We caught global \$_[0] = $_[0]\n"; }; } } __END__ In try block We caught global $@ = We caught lexical $_ = Substitute undefined variable We caught global $::_ = Substitute undefined variable We caught global $_[0] = Substitute undefined variable

        So summary, local $_ should NOT die but work just like our $_ inside given/when

        Try::Tiny documentation should lead with $::_

        I still don't think in terms of given/when -- well I do, but I spell it if/else/unless :D

        It works more sanely in Perl 5.18...

        In try block We caught Substitute undefined variable

        For Perl 5.10-5.16, the easiest fix is:

        catch { our $_; # stop lexical $_ from masking global $_ print "We caught $_\n"; }
        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1034160]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-10-01 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (389 votes), past polls