Skip to content

Instantly share code, notes, and snippets.

@AlexDaniel
Created July 18, 2018 01:35
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 AlexDaniel/f501ce8757fc74bc27fbc2f900c87e24 to your computer and use it in GitHub Desktop.
Save AlexDaniel/f501ce8757fc74bc27fbc2f900c87e24 to your computer and use it in GitHub Desktop.
run ‘perl6’, ‘sandbox/zzz’, 42
BEGIN @*ARGS = < -w -a -b c d -e >;
class MatchContext {
has $.name;
has $.available is rw;
}
class Option {
has $.name;
has $.supply is rw;
method callback() {
$!supply.tap(
-> $v {
given $v {
if .available {
if .name eq $!name {
.available = False;
}
}
}
}
);
}
}
sub getopt(@args is copy = @*ARGS) {
my $p = Supplier.new;
my @options = [
Option.new(name => "a", supply => $p.Supply),
Option.new(name => "b", supply => $p.Supply),
];
@options[0].callback();
@options[1].callback();
sub parse(Supply $source --> Supply) {
supply {
whenever $source {
if .starts-with('-') || .starts-with('--') {
note "In Parser: Emit the option {.Str}";
$p.emit(
my $mc = MatchContext.new(
name => .substr(1),
:available
));
emit "The {.Str} matched ? " ~ ($mc.available ?? 'FAILED' !! 'OK');
}
}
}
}
my $parse = parse(Supply.from-list(@args));
react {
whenever $parse -> $msg {
say "In MAIN: GOT MESSAGE: ", $msg;
}
}
note "done the supplier";
$p.done;
}
&getopt();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment