-
-
Save AlexDaniel/f501ce8757fc74bc27fbc2f900c87e24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run ‘perl6’, ‘sandbox/zzz’, 42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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