develooper Front page | perl.perl5.porters | Postings from March 2021

try/catch visibility to caller()

Thread Next
From:
Paul "LeoNerd" Evans
Date:
March 7, 2021 15:44
Subject:
try/catch visibility to caller()
Message ID:
20210307154354.56cafe0f@shy.leonerd.org.uk
There's an odd quirk about eval {} blocks, in that they are visible to
caller(), and hence turn up in the stack traces thrown by
Carp::longmess and friends:

  $ perl
  use Carp;
  sub C { Carp::cluck("Boo") }
  sub B { eval { C() } } 
  sub A { eval { B() } }
  A();
  Boo at - line 2.
          main::C() called at - line 3
          eval {...} called at - line 3
          main::B() called at - line 4
          eval {...} called at - line 4
          main::A() called at - line 5

That is sortof understandable if you consider that caller() is supposed
to tell us about successive levels of the "caller stack", where
"return" would send us.

But how caller() works internally is that it looks down the context
stack - which in addition to sub calls and eval {} frames, also
contains information about foreach loops and other fun things. Those
are invisible to caller().

A quirk of its current (experimental) implementation means that
try/catch blocks are also invisible to caller():

  $ bleadperl
  use experimental 'try';
  use Carp;
  sub C { Carp::cluck("Boo") }
  sub B { try { C() } catch($e) {} }
  sub A { try { B() } catch($e) {} }
  A();
  Boo at - line 3.
          main::C() called at - line 4
          main::B() called at - line 5
          main::A() called at - line 6

I think this is a useful property for them to have.

However, I didn't document or unit-test for it [1]. I'd like to propose
adding tests and documentation of that to ensure this property remains.

Do we agree?

(Cross-posted to github: https://github.com/Perl/perl5/issues/18628)

[1]: Actually until I wrote that example just now, I thought they
     *would* be visible so this mail was going to propose changing it.

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About