Skip to content

Instantly share code, notes, and snippets.

@alabamenhu
Created January 14, 2020 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alabamenhu/81eecb708148f53a530ececd437f80a9 to your computer and use it in GitHub Desktop.
Save alabamenhu/81eecb708148f53a530ececd437f80a9 to your computer and use it in GitHub Desktop.
Carp
unit module Carp;
my $BLOCK = False;
my $PRETTY = False;
sub carp($message?, --> Nil) {
# Ignores backtrace creation (2 frames) and carp() (1 frame)
my @frames = Backtrace.new(3).list;
my $line = @frames.head.line; # line of error
unless $BLOCK {
# Rewind to sub/method level
@frames.shift while @frames.head.code !~~ Routine;
}
my $type = @frames.head.code.^name.lc // '<unit>';
my $file = @frames.head.file // 'REPLY';
my $name = @frames.head.subname // '<unit>';
if $PRETTY {
$*ERR.say: "🐟 . o O ( { $message// "carp" } ) in $type $name at $file line $line";
} else {
$*ERR.say: ($message // "Carped: something's wrong") ~ "\n"
~ " in $type $name at $file line $line";
}
}
sub confess($message?) {
# Ignores backtrace creation (2 frames) and carp() (1 frame)
my @frames = Backtrace.new(3).list;
my $line = @frames.head.line; # line of error
unless $BLOCK {
# Rewind to sub/method level
@frames.shift while @frames.head.code !~~ Routine;
}
my $type = @frames.head.code.^name.lc;
my $file = @frames.head.file;
my $name = @frames.head.subname;
if $PRETTY {
$*ERR.say: "🙏 . o O ( { $message// "confesss" } )\n in $type $name at $file line $line";
$*ERR.say($_) for @frames; # output should match " in sub foo at bar.p6 line 0" format
} else {
$*ERR.say: ($message // "Confession: something's wrong") ~ "\n";
$*ERR.say($_) for @frames;
}
}
my package EXPORT::DEFAULT { OUR::{'&carp'} := &carp }
my package EXPORT::confess { OUR::{'&confess'} := &confess; say "confess not exported but ... here we are" }
my package EXPORT::block { $BLOCK = True }
my package EXPORT::ofun { say "didn't call ofun"; $PRETTY = True }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment