1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#!/usr/bin/env perl6 use v6; class Term::Choose { has Str $.prompt = 'Your Choice'; has Int @.mark; has %!backup_opt; # ... method choose ( Array $orig_list, %opt? ) { for %opt.keys -> $key { when $key eq 'prompt' { %!backup_opt{$key} = $!prompt; $!prompt = %opt{$key}; } when $key eq 'mark' { %!backup_opt{$key} = [ @!mark ]; @!mark = |%opt{$key}; } } say "prompt: $!prompt"; say "mark : @!mark[]\n"; self!rest_attributes(); } method !rest_attributes { for %!backup_opt.keys -> $key { when $key eq 'prompt' { $!prompt = %!backup_opt{$key} } when $key eq 'mark' { @!mark = |%!backup_opt{$key} } } } } my $n = Term::Choose.new( prompt => 'AAAAA', mark => [ 5, 6 ] ); $n.choose( [ 1 .. 10 ] ); $n.choose( [ 1 .. 10 ], { prompt => 'BBBBB', mark => [ 1 .. 4 ] } ); $n.choose( [ 1 .. 10 ] );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/usr/bin/env perl6 use v6; class Term::Choose { has Str $.prompt = 'Your Choice'; has Int @.mark; has %!opt; method choose ( Array $orig_list, %!opt? ) { %!opt<prompt> //= $.prompt; say "prompt: %!opt<prompt>" with %!opt<prompt>; %!opt<mark> //= [ @.mark ]; say "mark : %!opt<mark>[]\n" with %!opt<mark>; } } my $n = Term::Choose.new( prompt => 'AAAAA', mark => [ 5, 6 ] ); $n.choose( [ 1 .. 10 ] ); $n.choose( [ 1 .. 10 ], { prompt => 'BBBBB', mark => [ 1 .. 4 ] } ); $n.choose( [ 1 .. 10 ] );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#!/usr/bin/env perl6 use v6; class Term::Choose { has %.opt_global; has %!opt; method new ( %opt_global? ) { validate_options( %opt_global ); %opt_global<prompt> //= 'AAAAA'; %opt_global<justify> //= 1; self.bless( :%opt_global ); } sub valid_options { return { justify => '<[ 0 1 2 ]>', lf => 'Array', mark => 'Array', prompt => 'Str', }; }; sub validate_options ( %opt ) { return without %opt; my $valid = valid_options(); for %opt.kv -> $key, $value { when $valid{$key}:!exists { die "'$key' is not a valid option name"; } when ! defined $value { next; } when $valid{$key} eq 'Array' { die "$key => not an ARRAY reference." if ! $value.isa( Array ); die "$key => invalid array element" if $value !~~ /^<[0..9]>+$/; # Int; die "$key => too many array elemnts." if $key eq 'lf' && $value.elems > 2; } when $valid{$key} eq 'Str' { die "$key => not a string." if ! $value.isa( Str ); } when $value !~~ m/^{$valid{$key}}$/ { die "$key => '$value' is not a valid value."; } } } method choose ( Array $orig_list, %!opt? ) { validate_options( %!opt ); for %!opt_global.kv -> $key, $value { %!opt{$key} //= $value; } printf "%-7s: %s\n", .key, .value for %!opt.pairs; print "\n"; # do something with $orig_list } } my $n = Term::Choose.new( { mark => [ 4 .. 7 ] } ); $n.choose( [ 1 .. 10 ] ); $n.choose( [ 1 .. 10 ], { prompt => 'BBBBB', mark => [ 1 .. 3 ] } ); $n.choose( [ 1 .. 10 ] );
1 2 3 4 5 6 7 8 9 10
method new ( %opt_global? ) { self.bless( :%opt_global ); } submethod BUILD( :%opt_global ) { %!opt_global := %opt_global; validate_options( %!opt_global ); %!opt_global<prompt> //= 'AAAAA'; %!opt_global<justify> //= 1; }