package ILMT::Translator; use strict; use warnings; use Dir::Self; use Data::Dumper; use Exporter qw(import); use Module::Pluggable::Object; use Module::Runtime qw(use_module); our @EXPORT_OK = qw(get_translator get_langpairs); my %translator_table; sub new_translator { my $class = shift; my $self = { src => shift, tgt => shift, }; my $search_path = "ILMT::$self->{src}::$self->{tgt}"; @{$self->{plugins}} = map use_module($_), grep /^${search_path}::[^:]+$/, Module::Pluggable::Object->new(search_path => $search_path)->plugins; $self->{seq} = shift; bless $self, $class; # Register this module as a translator service $translator_table{$self->{src}}{$self->{tgt}} = $self; return $self; } sub get_translator { my ($src, $tgt) = @_; return $translator_table{$src}{$tgt}; } sub get_langpairs { print Dumper(\%translator_table); return map +(lc $_ => [ map lc, keys %{$translator_table{$_}} ]), keys %translator_table; } sub translate { my ($self, %args) = @_; my $result = ""; my @identifiers; my %final_result; #my $outfile = "all.out"; #open (fh, ">", "71.out"); my $mystr = ""; my @dispatch_seq = @{$self->{seq}}; foreach my $index (0 .. $#dispatch_seq) { my $module = $dispatch_seq[$index ++]; my $identifier = lc("${module}-$index"); push @identifiers, $identifier; print "module ## $module\n"; #$mystr .= "\n$module\n"; #print myfh "\n$module\n"; my $package = "ILMT::$self->{src}::$self->{tgt}::$module"; $args{$identifier} = $package->can('process')->(%args); $args{'data'} = $args{$identifier}; #print myfh "$args{'data'}\n"; #$mystr .= "$args{'data'}\n"; #print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"; #print "translator data :: \n"; #print $args{'data'}; #print "\n"; } #binmode STDOUT, ":utf8"; #print fh $mystr; #close(fh); @final_result{@identifiers} = @args{@identifiers}; return \%final_result; } sub partial_p { my ($self, $start, $end, %args) = @_; my $result = ""; my @dispatch_seq = @{$self->{seq}}; my @identifiers; my %final_result; foreach my $index ($start .. $end) { my $module = $dispatch_seq[$index - 1]; my $identifier = lc("${module}-$index"); push @identifiers, $identifier; print "module ## $module\n"; my $package = "ILMT::$self->{src}::$self->{tgt}::$module"; $args{$identifier} = $package->can('process')->(%args); $args{'data'} = $args{$identifier}; #print "translator data :: \n"; #print Dumper($args{'data'}); #print "\n"; } @final_result{@identifiers} = @args{@identifiers}; return \%final_result; } 1;