| 1 | package POE::Component::Lingua::Translate; | ||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | $POE::Component::Lingua::Translate::AUTHORITY = 'cpan:HINRIK'; | |||||||||||
| 4 | } | ||||||||||||
| 5 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | $POE::Component::Lingua::Translate::VERSION = '0.06'; | |||||||||||
| 7 | } | ||||||||||||
| 8 | |||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use POE; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use POE::Component::Generic; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 16 | 0 | my ($package, %args) = @_; | |||||||||||
| 17 | 0 | my $self = bless { }, $package; | |||||||||||
| 18 | 0 | ▶ | $self->{alias} = delete $args{alias} if $args{alias}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 19 | 0 | $self->{trans_args} = \%args; | |||||||||||
| 20 | |||||||||||||
| 21 | 0 | POE::Session->create( | |||||||||||
| 22 | object_states => [ | ||||||||||||
| 23 | $self => { | ||||||||||||
| 24 | # public events | ||||||||||||
| 25 | translate => '_translate', | ||||||||||||
| 26 | shutdown => '_shutdown', | ||||||||||||
| 27 | }, | ||||||||||||
| 28 | $self => [ qw(_start _stop _result) ], | ||||||||||||
| 29 | ], | ||||||||||||
| 30 | ); | ||||||||||||
| 31 | |||||||||||||
| 32 | 0 | return $self; | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 0 | ▶ | sub _start { | ||||||||||
|
Subroutine
_start: not called CC=2 SCAR=17.9
| |||||||||||||
| 36 | 0 | my ($session, $self) = @_[SESSION, OBJECT]; | |||||||||||
| 37 | 0 | $self->{session_id} = $session->ID(); | |||||||||||
| 38 | |||||||||||||
| 39 | 0 | ▶ | if ( $self->{alias} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 0 | $poe_kernel->alias_set( $self->{alias} ); | |||||||||||
| 41 | } | ||||||||||||
| 42 | else { | ||||||||||||
| 43 | 0 | $poe_kernel->refcount_increment( $self->{session_id} => __PACKAGE__ ); | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | $self->{trans} = POE::Component::Generic->spawn( | ||||||||||||
| 47 | package => 'Lingua::Translate', | ||||||||||||
| 48 | 0 | object_options => [ %{ $self->{trans_args} } ], | |||||||||||
| 49 | methods => [ qw(translate) ], | ||||||||||||
| 50 | verbose => 1, | ||||||||||||
| 51 | ); | ||||||||||||
| 52 | |||||||||||||
| 53 | 0 | return; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | 0 | ▶ | sub _stop { | ||||||||||
|
Subroutine
_stop: not called CC=1 SCAR=6.9
| |||||||||||||
| 57 | 0 | return; | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 0 | ▶ | sub _shutdown { | ||||||||||
|
Subroutine
_shutdown: not called CC=3 SCAR=24.8
| |||||||||||||
| 61 | 0 | my $self = $_[OBJECT]; | |||||||||||
| 62 | 0 | $poe_kernel->alias_remove( $_ ) for $poe_kernel->alias_list(); | |||||||||||
| 63 | 0 | ▶ | $poe_kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) if !$self->{alias}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 0 | return; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 0 | ▶ | sub _translate { | ||||||||||
|
Subroutine
_translate: not called CC=2 SCAR=17.9
| |||||||||||||
| 68 | 0 | my ($self, $sender, $text, $context) = @_[OBJECT, SENDER, ARG0, ARG1]; | |||||||||||
| 69 | |||||||||||||
| 70 | $self->{trans}->yield( | ||||||||||||
| 71 | 0 | ▶ | translate => | ||||||||||
|
Condition0%0/2
$context || {}
MC/DC0%0/1
$context || {}
$context
Truth table0%0/2
A
A= $context
| |||||||||||||
| 72 | { | ||||||||||||
| 73 | event => '_result', | ||||||||||||
| 74 | data => { | ||||||||||||
| 75 | recipient => $sender->ID(), | ||||||||||||
| 76 | context => $context || { }, | ||||||||||||
| 77 | }, | ||||||||||||
| 78 | }, | ||||||||||||
| 79 | $text, | ||||||||||||
| 80 | ); | ||||||||||||
| 81 | 0 | return; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 0 | ▶ | sub _result { | ||||||||||
|
Subroutine
_result: not called CC=2 SCAR=17.9
| |||||||||||||
| 85 | 0 | my ($ref, $result) = @_[ARG0, ARG1]; | |||||||||||
| 86 | |||||||||||||
| 87 | 0 | my ($recipient, $context) = @{ $ref->{data} }{ qw(recipient context) }; | |||||||||||
| 88 | $poe_kernel->post( | ||||||||||||
| 89 | $recipient, | ||||||||||||
| 90 | 'translated', | ||||||||||||
| 91 | $result, | ||||||||||||
| 92 | $context, | ||||||||||||
| 93 | 0 | ▶ | ($ref->{error} ? $ref->{error} : ()) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | ); | ||||||||||||
| 95 | |||||||||||||
| 96 | 0 | return; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | 0 | ▶ | sub session_id { | ||||||||||
|
Subroutine
session_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 100 | 0 | my ($self) = @_; | |||||||||||
| 101 | 0 | return $self->{session_id}; | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | 1 | 1; | |||||||||||
| 105 | |||||||||||||
| 106 | =encoding utf8 | ||||||||||||
| 107 | |||||||||||||
| 108 | =head1 NAME | ||||||||||||
| 109 | |||||||||||||
| 110 | POE::Component::Lingua::Translate - A non-blocking wrapper around L<Lingua::Translate|Lingua::Translate> | ||||||||||||
| 111 | |||||||||||||
| 112 | =head1 SYNOPSIS | ||||||||||||
| 113 | |||||||||||||
| 114 | use POE; | ||||||||||||
| 115 | use POE::Component::Lingua::Translate; | ||||||||||||
| 116 | |||||||||||||
| 117 | POE::Session->create( | ||||||||||||
| 118 | package_states => [ | ||||||||||||
| 119 | main => [ qw(_start translated) ], | ||||||||||||
| 120 | ], | ||||||||||||
| 121 | ); | ||||||||||||
| 122 | |||||||||||||
| 123 | $poe_kernel->run(); | ||||||||||||
| 124 | |||||||||||||
| 125 | sub _start { | ||||||||||||
| 126 | my $heap = $_[HEAP]; | ||||||||||||
| 127 | $heap->{trans} = POE::Component::Lingua::Translate->new( | ||||||||||||
| 128 | alias => 'translator', | ||||||||||||
| 129 | back_end => 'Babelfish', | ||||||||||||
| 130 | src => 'en', | ||||||||||||
| 131 | dest => 'de', | ||||||||||||
| 132 | ); | ||||||||||||
| 133 | |||||||||||||
| 134 | $poe_kernel->post(translator => translate => 'This is a sentence'); | ||||||||||||
| 135 | return; | ||||||||||||
| 136 | } | ||||||||||||
| 137 | |||||||||||||
| 138 | sub translated { | ||||||||||||
| 139 | my $result = $_[ARG0]; | ||||||||||||
| 140 | # prints 'Dieses ist ein Satz' | ||||||||||||
| 141 | print $result . "\n"; | ||||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | =head1 DESCRIPTION | ||||||||||||
| 145 | |||||||||||||
| 146 | POE::Component::Lingua::Translate is a L<POE> component that provides a | ||||||||||||
| 147 | non-blocking wrapper around L<Lingua::Translate|Lingua::Translate>. It accepts | ||||||||||||
| 148 | C<translate> events and emits C<translated> events back. | ||||||||||||
| 149 | |||||||||||||
| 150 | =head1 CONSTRUCTOR | ||||||||||||
| 151 | |||||||||||||
| 152 | =over | ||||||||||||
| 153 | |||||||||||||
| 154 | =item C<new> | ||||||||||||
| 155 | |||||||||||||
| 156 | Arguments | ||||||||||||
| 157 | |||||||||||||
| 158 | 'alias', an optional alias for the component's session. | ||||||||||||
| 159 | |||||||||||||
| 160 | Any other arguments will be passed verbatim to L<Lingua::Translate|Lingua::Translate>'s | ||||||||||||
| 161 | constructor. | ||||||||||||
| 162 | |||||||||||||
| 163 | =back | ||||||||||||
| 164 | |||||||||||||
| 165 | =head1 METHODS | ||||||||||||
| 166 | |||||||||||||
| 167 | =over | ||||||||||||
| 168 | |||||||||||||
| 169 | =item C<session_id> | ||||||||||||
| 170 | |||||||||||||
| 171 | Takes no arguments. Returns the POE Session ID of the component. | ||||||||||||
| 172 | |||||||||||||
| 173 | =back | ||||||||||||
| 174 | |||||||||||||
| 175 | =head1 INPUT | ||||||||||||
| 176 | |||||||||||||
| 177 | The POE events this component will accept. | ||||||||||||
| 178 | |||||||||||||
| 179 | =over | ||||||||||||
| 180 | |||||||||||||
| 181 | =item C<translate> | ||||||||||||
| 182 | |||||||||||||
| 183 | The first argument should be a string containing some text to translate. The | ||||||||||||
| 184 | second argument (optional) can be a hash reference containing some context | ||||||||||||
| 185 | information. You'll get this hash reference back with the C<translated> event. | ||||||||||||
| 186 | |||||||||||||
| 187 | =item C<shutdown> | ||||||||||||
| 188 | |||||||||||||
| 189 | Takes no arguments, terminates the component. | ||||||||||||
| 190 | |||||||||||||
| 191 | =back | ||||||||||||
| 192 | |||||||||||||
| 193 | =head1 OUTPUT | ||||||||||||
| 194 | |||||||||||||
| 195 | The POE events emitted by this component. | ||||||||||||
| 196 | |||||||||||||
| 197 | =over | ||||||||||||
| 198 | |||||||||||||
| 199 | =item C<translated> | ||||||||||||
| 200 | |||||||||||||
| 201 | ARG0 is the translated text. ARG1 is the context hashref from C<translate>. If | ||||||||||||
| 202 | there was an error, ARG2 will be the error string. | ||||||||||||
| 203 | |||||||||||||
| 204 | =back | ||||||||||||
| 205 | |||||||||||||
| 206 | =head1 AUTHOR | ||||||||||||
| 207 | |||||||||||||
| 208 | Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com | ||||||||||||
| 209 | |||||||||||||
| 210 | =head1 LICENSE AND COPYRIGHT | ||||||||||||
| 211 | |||||||||||||
| 212 | Copyright 2008 Hinrik E<Ouml>rn SigurE<eth>sson | ||||||||||||
| 213 | |||||||||||||
| 214 | This program is free software, you can redistribute it and/or modify | ||||||||||||
| 215 | it under the same terms as Perl itself. | ||||||||||||
| 216 | |||||||||||||
| 217 | =cut | ||||||||||||