1 2 3 4 5 6
use Term::Choose qw( choose ); my $d = choose( [ 1 .. 20 ] ); # scalar context: ein Element kann ausgewählt werden my @a = choose( [ 1 .. 20 ] ); # list context: mehrere Elemente können ausgewählt werden
1 2 3 4 5 6 7 8 9 10 11
use v6; use Term::Choose; choose( [ 1 .. 20 ], { return => 0 } ); # return == 0 gibt nichts zurück my $d = choose( [ 1 .. 20 ], { return => 0 } ); # return == 1 ein Element my @a = choose( [ 1 .. 20 ] ); # default: return => 2 # return == 2 mehrere Elemente
1 2 3
my $nr = 5; my @d = choose( [ 1 .. 20 ], { max_ret => $nr } ); # man kann maximal $nr Elemente auswählen - default unbegrenzt
choose( [ 'Fehlermeldung' ], { prompt => 'ENTER to continue' } );
print_and_wait
read_and_continue
my $idx = choose( [ 'a' .. 'z' ], { index => 1 } );
1 2 3 4 5 6 7 8 9
sub choose ( Array $orig_list, %opt? ) is export { %opt<multiselect> = 0; return Term::Choose.new().choose( $orig_list, %opt ); } sub choose_multi ( Array $orig_list, %opt? ) is export { %opt<multiselect> = 1; return Term::Choose.new().choose( $orig_list, %opt ); }
1 2 3 4 5 6 7
sub choose ( Array $orig_list, %opt? ) is export { return Term::Choose.new( multiselect => 0 ).choose( $orig_list, %opt ); } sub choose_multi ( Array $orig_list, %opt? ) is export { return Term::Choose.new( multiselect => 1 ).choose( $orig_list, %opt ); }
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
has $!multiselect; sub choose ( Array $orig_list, %opt? ) is export { return Term::Choose.new().choose( $orig_list, %opt ); } sub choose_multi ( Array $orig_list, %opt? ) is export { return Term::Choose.new().choose_multi( $orig_list, %opt ); } method choose () { $!multiselect = 0; self._my_inner_method( ... ); } method choose_multi () { $!multiselect = 1; self._my_inner_method( ... ); } method _my_inner_method ( ... ) { ... ... if $!multiselect { ... } else { ... } }
1 2 3 4 5 6
method choose_multi ( Array $orig_list, %opt? ) { return if ! $orig_list.elems; validate_options( %opt ); %opt<multiselect> = 1; return self!_private_choose( $orig_list, %opt ); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
sub choose_multi ( Array $orig_list, %opt? ) is export { return Term::Choose.new().choose_multi( $orig_list, %opt ); } # bisschen weniger Code # einen Wrapper mehr sub choose_multi ( Array $orig_list, %opt? ) is export { return if ! $orig_list.elems; validate_options( %opt ); %opt<multiselect> = 1; return Term::Choose.new()!_private_choose( $orig_list, %opt ); } # bisschen mehr Code # einen Wrapper weniger
return self!_private_choose( $orig_list, %opt, { Multi => 1 } );