| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::Gimei::Runner; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 7 |  |  | 7 |  | 13142 | use warnings; | 
|  | 7 |  |  |  |  | 43 |  | 
|  | 7 |  |  |  |  | 174 |  | 
| 4 | 7 |  |  | 7 |  | 59 | use v5.22; | 
|  | 7 |  |  |  |  | 18 |  | 
| 5 |  |  |  |  |  |  | binmode STDOUT, ":utf8"; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 7 |  |  | 7 |  | 4205 | use Getopt::Long; | 
|  | 7 |  |  |  |  | 79473 |  | 
|  | 7 |  |  |  |  | 35 |  | 
| 8 | 7 |  |  | 7 |  | 3776 | use Pod::Usage; | 
|  | 7 |  |  |  |  | 488185 |  | 
|  | 7 |  |  |  |  | 774 |  | 
| 9 | 7 |  |  | 7 |  | 61 | use Pod::Find qw( pod_where ); | 
|  | 7 |  |  |  |  | 15 |  | 
|  | 7 |  |  |  |  | 354 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 7 |  |  | 7 |  | 2525 | use App::Gimei; | 
|  | 7 |  |  |  |  | 16 |  | 
|  | 7 |  |  |  |  | 168 |  | 
| 12 | 7 |  |  | 7 |  | 2714 | use Data::Gimei; | 
|  | 7 |  |  |  |  | 237906 |  | 
|  | 7 |  |  |  |  | 184 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 7 |  |  | 7 |  | 43 | use Class::Tiny; | 
|  | 7 |  |  |  |  | 12 |  | 
|  | 7 |  |  |  |  | 25 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # | 
| 17 |  |  |  |  |  |  | # global vars | 
| 18 |  |  |  |  |  |  | # | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | my %conf = ( POD_FILE => pod_where( { -inc => 1 }, 'App::Gimei' ) ); | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | # | 
| 23 |  |  |  |  |  |  | # methods | 
| 24 |  |  |  |  |  |  | # | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | sub parse_option { | 
| 27 | 36 |  |  | 36 | 0 | 75 | my ( $self, $args_ref, $opts_ref ) = @_; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 36 |  |  |  |  | 66 | $opts_ref->{n}   = 1; | 
| 30 | 36 |  |  |  |  | 66 | $opts_ref->{sep} = ', '; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 36 |  |  |  |  | 205 | my $p = Getopt::Long::Parser->new( config => ["no_ignore_case"], ); | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 36 |  |  | 1 |  | 2686 | local $SIG{__WARN__} = sub { die "Error: $_[0]" }; | 
|  | 1 |  |  |  |  | 552 |  | 
| 35 | 36 |  |  |  |  | 131 | my $ok = $p->getoptionsfromarray( $args_ref, $opts_ref, "help|h", "version|v", "n=i", | 
| 36 |  |  |  |  |  |  | "sep=s", ); | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 35 | 100 |  |  |  | 12742 | if ( $opts_ref->{n} < 1 ) { | 
| 39 | 1 |  |  |  |  | 13 | die | 
| 40 |  |  |  |  |  |  | "Error: value $opts_ref->{n} invalid for option n (must be positive number)\n"; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub execute { | 
| 45 | 36 |  |  | 36 | 0 | 111938 | my ( $self, @args ) = @_; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 36 |  |  |  |  | 64 | my %opts; | 
| 48 | 36 |  |  |  |  | 156 | $self->parse_option( \@args, \%opts ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 34 | 100 |  |  |  | 101 | if ( $opts{version} ) { | 
| 51 | 2 |  |  |  |  | 109 | say "$App::Gimei::VERSION"; | 
| 52 | 2 |  |  |  |  | 16 | return 0; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 32 | 100 |  |  |  | 76 | if ( $opts{help} ) { | 
| 56 | 2 |  |  |  |  | 10 | pod2usage( -input => $conf{POD_FILE}, -exitval => 'noexit' ); | 
| 57 | 2 |  |  |  |  | 24847 | return 0; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 30 | 100 |  |  |  | 63 | if ( !@args ) { | 
| 61 | 3 |  |  |  |  | 8 | push @args, 'name:kanji'; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 30 |  |  |  |  | 84 | foreach ( 1 .. $opts{n} ) { | 
| 65 | 31 |  |  |  |  | 262 | my %words = ( | 
| 66 |  |  |  |  |  |  | name    => Data::Gimei::Name->new(), | 
| 67 |  |  |  |  |  |  | male    => Data::Gimei::Name->new( gender => 'male' ), | 
| 68 |  |  |  |  |  |  | female  => Data::Gimei::Name->new( gender => 'female' ), | 
| 69 |  |  |  |  |  |  | address => Data::Gimei::Address->new() | 
| 70 |  |  |  |  |  |  | ); | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 31 |  |  |  |  | 369915 | my @results; | 
| 73 | 31 |  |  |  |  | 67 | foreach my $arg (@args) { | 
| 74 | 32 |  |  |  |  | 133 | my @tokens = split( /[-:]/, $arg ); | 
| 75 | 32 |  |  |  |  | 83 | push @results, execute_tokens( \@tokens, \%words ); | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 26 |  |  |  |  | 2171 | say join $opts{sep}, @results; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 25 |  |  |  |  | 2232 | return 0; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | # | 
| 85 |  |  |  |  |  |  | # functions ... | 
| 86 |  |  |  |  |  |  | # | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | # ARG:                   [WORD_TYPE] [':' WORD_SUB_TYPE] [':' RENDERING] | 
| 89 |  |  |  |  |  |  | # WORD_TYPE:             'name'       | 'address' | 
| 90 |  |  |  |  |  |  | # WORD_SUBTYPE(name):    'family'     | 'given' | 
| 91 |  |  |  |  |  |  | # WORD_SUBTYPE(address): 'prefecture' | 'city'     | 'town' | 
| 92 |  |  |  |  |  |  | # RENDERING:             'kanji'      | 'hiragana' | 'katakana' | 'romaji' | 
| 93 |  |  |  |  |  |  | sub execute_tokens { | 
| 94 | 32 |  |  | 32 | 0 | 57 | my ( $tokens_ref, $words_ref ) = @_; | 
| 95 | 32 |  |  |  |  | 75 | my ( $word_type, $word, $token ); | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 32 |  |  |  |  | 58 | $token = shift @$tokens_ref; | 
| 98 | 32 | 100 | 100 |  |  | 172 | if ( $token eq 'name' || $token eq 'male' || $token eq 'female' ) { | 
|  |  | 100 | 100 |  |  |  |  | 
| 99 | 20 |  |  |  |  | 50 | ( $word, $word_type ) = subtype_name( $tokens_ref, $words_ref->{$token} ); | 
| 100 |  |  |  |  |  |  | } elsif ( $token eq 'address' ) { | 
| 101 | 11 |  |  |  |  | 33 | ( $word, $word_type ) = subtype_address( $tokens_ref, $words_ref->{$token} ); | 
| 102 |  |  |  |  |  |  | } else { | 
| 103 | 1 |  |  |  |  | 9 | die "Error: unknown word_type: $token\n"; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 31 |  |  |  |  | 71 | return render( $tokens_ref, $word_type, $word ); | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | sub subtype_name { | 
| 110 | 20 |  |  | 20 | 0 | 38 | my ( $tokens_ref, $word ) = @_; | 
| 111 | 20 |  |  |  |  | 29 | my ( $token, $subtype, $call, $word_type ); | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 20 |  |  |  |  | 137 | my %map = ( | 
| 114 |  |  |  |  |  |  | 'family' => [ 'surname',  'name'   ], | 
| 115 |  |  |  |  |  |  | 'last'   => [ 'surname',  'name'   ], | 
| 116 |  |  |  |  |  |  | 'given'  => [ 'forename', 'name'   ], | 
| 117 |  |  |  |  |  |  | 'first'  => [ 'forename', 'name'   ], | 
| 118 |  |  |  |  |  |  | 'gender' => [ 'gender',   'gender' ], | 
| 119 |  |  |  |  |  |  | 'sex'    => [ 'gender',   'gender' ], | 
| 120 |  |  |  |  |  |  | ); | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 20 |  |  |  |  | 34 | $word_type = 'name'; | 
| 123 | 20 |  | 100 |  |  | 84 | $token     = @$tokens_ref[0] // ''; | 
| 124 | 20 | 100 |  |  |  | 52 | if ( my $m = $map{$token} ) { | 
| 125 | 10 |  |  |  |  | 19 | shift @$tokens_ref; | 
| 126 | 10 | 50 |  |  |  | 47 | $call      = $word->can( $m->[0] ) or die "system err"; | 
| 127 | 10 |  |  |  |  | 207 | $word      = $word->$call(); | 
| 128 | 10 |  |  |  |  | 60 | $word_type = $m->[1]; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 20 |  |  |  |  | 79 | return ( $word, $word_type ); | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | sub subtype_address { | 
| 135 | 11 |  |  | 11 | 0 | 22 | my ( $tokens_ref, $word ) = @_; | 
| 136 |  |  |  |  |  |  |  | 
| 137 | 11 |  | 100 |  |  | 31 | my $token = @$tokens_ref[0] // ''; | 
| 138 | 11 | 100 | 100 |  |  | 54 | if ( $token eq 'prefecture' || $token eq 'city' || $token eq 'town' ) { | 
|  |  |  | 100 |  |  |  |  | 
| 139 | 7 |  |  |  |  | 10 | shift @$tokens_ref; | 
| 140 | 7 |  |  |  |  | 25 | my $call = $word->can($token); | 
| 141 | 7 | 50 |  |  |  | 17 | die "system error" if ( !$call ); | 
| 142 | 7 |  |  |  |  | 119 | $word = $word->$call(); | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 | 11 |  |  |  |  | 68 | return ( $word, 'address' ); | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | # romaji not supported in WORD_TYPE = 'address' | 
| 149 |  |  |  |  |  |  | sub render { | 
| 150 | 31 |  |  | 31 | 0 | 55 | my ( $tokens_ref, $word_type, $word ) = @_; | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 31 |  |  |  |  | 52 | my $token = @$tokens_ref[0]; | 
| 153 | 31 | 100 | 100 |  |  | 93 | if ( !$token || $token eq 'name' ) { | 
| 154 | 21 |  |  |  |  | 33 | $token = "kanji"; | 
| 155 |  |  |  |  |  |  | } | 
| 156 |  |  |  |  |  |  |  | 
| 157 | 31 | 100 | 100 |  |  | 85 | if ( $word_type eq 'address' && $token eq 'romaji' ) { | 
| 158 | 2 |  |  |  |  | 18 | die "Error: unknown subtype or rendering: $token\n"; | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  |  | 
| 161 | 29 | 100 |  |  |  | 54 | if ( $word_type eq 'gender' ) { | 
| 162 | 3 |  |  |  |  | 12 | return $word; | 
| 163 |  |  |  |  |  |  | } | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 26 |  |  |  |  | 88 | my $call = $word->can($token); | 
| 166 | 26 | 100 |  |  |  | 63 | die "Error: unknown subtype or rendering: $token\n" if ( !$call ); | 
| 167 |  |  |  |  |  |  |  | 
| 168 | 24 |  |  |  |  | 222 | return $word->$call(); | 
| 169 |  |  |  |  |  |  | } | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | 1; |