Skip to content

Instantly share code, notes, and snippets.

@gfldex
Created July 29, 2020 17:56
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 gfldex/1193b1eaf86f6be0903f92541a0adad3 to your computer and use it in GitHub Desktop.
Save gfldex/1193b1eaf86f6be0903f92541a0adad3 to your computer and use it in GitHub Desktop.
use v6;
class Shell::Piping::Exitcode {
has $.exitint;
has $.command;
has @.STDERR;
multi method ACCEPTS(::?CLASS:D: Numeric $rhs) {
$!exitint ~~ $rhs
}
multi method ACCEPTS(::?CLASS:D: Str $rhs) {
$!command ~~ $rhs
}
multi method ACCEPTS(::?CLASS:D: Regex $rhs) {
@!STDERR.join(„\n“) ~~ $rhs
}
}
use MONKEY-TYPING;
augment class Regex {
multi method ACCEPTS(Regex:D: Shell::Piping::Exitcode:D $ex) {
use nqp;
$/ := nqp::getlexcaller('$/');
# $/ := CALLER::<$/>;
my $m = $ex.STDERR.join(„\n“).match(self);
?$m
}
}
augment class Int {
multi method ACCEPTS(Int:D: Shell::Piping::Exitcode:D $ex) {
self.ACCEPTS($ex.exitint)
}
}
augment class Str {
multi method ACCEPTS(Str:D: Shell::Piping::Exitcode:D $ex) {
self.ACCEPTS($ex.command)
}
}
constant Exitcode = Shell::Piping::Exitcode;
my $ex = Exitcode.new: :STDERR(<abc def ghi>), :exitint(42), :command<find>;
say $ex ~~ 42 & ‚find‘ & /def/;
say $ex ~~ /def/;
given $ex {
when 42 & ‚find‘ & /def\s(\S+)/ {
say $/;
note „find terminated with 42 and $0“;
}
}
LEAVE slurp($*PROGRAM).say;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment