File Coverage

blib/lib/POE/Component/Lingua/Translate.pm
Criterion Covered Total %
statement 12 14 85.7
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package POE::Component::Lingua::Translate;
2             BEGIN {
3 1     1   1072 $POE::Component::Lingua::Translate::AUTHORITY = 'cpan:HINRIK';
4             }
5             BEGIN {
6 1     1   19 $POE::Component::Lingua::Translate::VERSION = '0.06';
7             }
8              
9 1     1   10 use strict;
  1         2  
  1         40  
10 1     1   5 use warnings FATAL => 'all';
  1         12  
  1         50  
11 1     1   6 use Carp;
  1         2  
  1         105  
12 1     1   410 use POE;
  0            
  0            
13             use POE::Component::Generic;
14              
15             sub new {
16             my ($package, %args) = @_;
17             my $self = bless { }, $package;
18             $self->{alias} = delete $args{alias} if $args{alias};
19             $self->{trans_args} = \%args;
20            
21             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             return $self;
33             }
34              
35             sub _start {
36             my ($session, $self) = @_[SESSION, OBJECT];
37             $self->{session_id} = $session->ID();
38              
39             if ( $self->{alias} ) {
40             $poe_kernel->alias_set( $self->{alias} );
41             }
42             else {
43             $poe_kernel->refcount_increment( $self->{session_id} => __PACKAGE__ );
44             }
45              
46             $self->{trans} = POE::Component::Generic->spawn(
47             package => 'Lingua::Translate',
48             object_options => [ %{ $self->{trans_args} } ],
49             methods => [ qw(translate) ],
50             verbose => 1,
51             );
52            
53             return;
54             }
55              
56             sub _stop {
57             return;
58             }
59              
60             sub _shutdown {
61             my $self = $_[OBJECT];
62             $poe_kernel->alias_remove( $_ ) for $poe_kernel->alias_list();
63             $poe_kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) if !$self->{alias};
64             return;
65             }
66              
67             sub _translate {
68             my ($self, $sender, $text, $context) = @_[OBJECT, SENDER, ARG0, ARG1];
69              
70             $self->{trans}->yield(
71             translate =>
72             {
73             event => '_result',
74             data => {
75             recipient => $sender->ID(),
76             context => $context || { },
77             },
78             },
79             $text,
80             );
81             return;
82             }
83              
84             sub _result {
85             my ($ref, $result) = @_[ARG0, ARG1];
86              
87             my ($recipient, $context) = @{ $ref->{data} }{ qw(recipient context) };
88             $poe_kernel->post(
89             $recipient,
90             'translated',
91             $result,
92             $context,
93             ($ref->{error} ? $ref->{error} : ())
94             );
95            
96             return;
97             }
98              
99             sub session_id {
100             my ($self) = @_;
101             return $self->{session_id};
102             }
103              
104             1;
105              
106             =encoding utf8
107              
108             =head1 NAME
109              
110             POE::Component::Lingua::Translate - A non-blocking wrapper around L
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 component that provides a
147             non-blocking wrapper around L. It accepts
148             C events and emits C events back.
149              
150             =head1 CONSTRUCTOR
151              
152             =over
153              
154             =item C
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's
161             constructor.
162              
163             =back
164              
165             =head1 METHODS
166              
167             =over
168              
169             =item C
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
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 event.
186              
187             =item C
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
200              
201             ARG0 is the translated text. ARG1 is the context hashref from C. If
202             there was an error, ARG2 will be the error string.
203              
204             =back
205              
206             =head1 AUTHOR
207              
208             Hinrik Ern SigurEsson, hinrik.sig@gmail.com
209              
210             =head1 LICENSE AND COPYRIGHT
211              
212             Copyright 2008 Hinrik Ern SigurEsson
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