Carp::REPL(3)

πŸ“¦
version 0.18 Β· 2 versions
, in fedora 43

πŸ’– Carp::REPL πŸ’–

okay so bestie, welcome to Carp::REPL β€” that's the Perl module living in the User Contributed Perl Documentation section of your manual (you'd have found it as Carp::REPL(3) in the header, slay). What does it do? It gives you a read-eval-print-loop on die and/or warn. Like, your program crashes and instead of just dying dramatically, you get a whole interactive vibe check. iconic.


✨ NAME ✨

Carp::REPL β€” read-eval-print-loop on die and/or warn. periodt.


🌸 SYNOPSIS 🌸

the intended way to use this module is through the command line. (iykyk)

perl -MCarp::REPL tps-report.pl
    Can't call method "cover_sheet" without a package or object reference at tps-report.pl line 6019.

and then, instead of exiting, you get a REPL! bestie that's the whole point πŸ’…

$ $form
27B/6

$ $self->get_form
27B/6

$ "ah ha! there's my bug, I thought get_form returned an object"
ah ha! there's my bug, I thought get_form returned an object

πŸ¦‹ USAGE πŸ¦‹

πŸ’œ -MCarp::REPL

πŸ’œ -MCarp::REPL=warn

works as a command line argument. it automatically installs the die handler for you, so if you get a fatal error you get a REPL before the universe explodes. specifying =warn also installs a warn handler, for sniffing out those mysterious warnings. obsessed.

πŸ’œ use Carp::REPL;

πŸ’œ use Carp::REPL 'warn';

same as above, fr fr.

πŸ’œ use Carp::REPL 'nodie';

loads the module without installing the die handler. use this if you just wanna run Carp::REPL::repl on your own terms, main character energy.

πŸ’œ use Carp::REPL 'test';

πŸ’œ -MCarp::REPL=test

load a REPL on test failure! (as long as it uses ok in Test::More). yes bestie, your failing tests get a whole glow-up.


⭐ FUNCTIONS ⭐

πŸŽ€ repl

this module's interface consists of exactly one function: repl. it's provided so you may install your own $SIG{__DIE__} handler if you have no alternatives.

it takes the same arguments as die, and returns no useful value. in fact, don't even depend on it returning at all! like, it might just dip.

one useful place for calling this manually is if you just wanna check the state of things without having to throw a fake error. you can also change any variables and those changes will be seen by the rest of your program. lowkey powerful.

use Carp::REPL 'repl';

sub involved_calculation {
    # ...
    $d = maybe_zero();
    # ...
    repl(); # $d = 1
    $sum += $n / $d;
    # ...
}

unfortunately, if you instead go with the usual -MCarp::REPL, then $SIG{__DIE__} will be invoked and there's no general way to recover. 😭 but you can still change variables to poke at things. it's giving "not ideal but we work with it".


πŸ‘‘ COMMANDS πŸ‘‘

note that this is not supposed to be a full-fledged debugger. a few commands are provided to aid you in finding out what went awry. see Devel::ebug if you're looking for a serious debugger. this one's just here for the plot.

  • :u β€” moves one frame up in the stack.
  • :d β€” moves one frame down in the stack.
  • :top β€” moves to the top frame of the stack.
  • :bottom β€” moves to the bottom frame of the stack.
  • :t β€” redisplay the stack trace.
  • :e β€” display the current lexical environment.
  • :l β€” list eleven lines of source code of the current frame.
  • :q β€” close the REPL. (^D also works)

🌈 VARIABLES 🌈

  • $_REPL β€” this represents the Devel::REPL object.
  • $_a β€” this represents the arguments passed to the subroutine at the current frame in the call stack. modifications are ignored (how would that work anyway?? re-invoke the sub?? bestie pls).

πŸ’€ CAVEATS πŸ’€

dynamic scope probably produces unexpected results. i don't see any easy (or even difficult!) solution to this. therefore it's a caveat and not a bug. :) we love a self-aware disclaimer.

heads up: dynamic scope + unexpected results = chaos. it's documented on purpose, not fixed on purpose. iykyk.

🧚 SEE ALSO 🧚

Devel::REPL, Devel::ebug, Enbugger, CGI::Inspect


πŸ’Œ AUTHOR πŸ’Œ

Shawn M Moore, "<sartak at gmail.com>"


πŸ™ˆ BUGS πŸ™ˆ

please report any bugs or feature requests to "bug-carp-repl at rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Carp-REPL>.


🫢 ACKNOWLEDGEMENTS 🫢

thanks to Nelson Elhage and Jesse Vincent for the idea.

thanks to Matt Trout and Stevan Little for their advice.


copyright 2007-2008 Best Practical Solutions.

this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page, bestie πŸ’… this gorgeousness dropped with perl v5.42.0 and is dated 2025-07-25 β€” go forth and REPL responsibly, no cap πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from