Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: chunking up texts correctly for online translation

by Aldebaran (Curate)
on Jun 30, 2019 at 18:12 UTC ( [id://11102200]=note: print w/replies, xml ) Need Help??


in reply to Re^2: chunking up texts correctly for online translation
in thread chunking up texts correctly for online translation

I've been looking at the source for the pm and wonder if I shouldn't imitate it in significant ways. source listing for Translate.pm For example, should I use Readonly for these values?

my ( $REST_HOST, $REST_URL, $CONSOLE_URL, %SIZE_LIMIT_FOR ); { Readonly $REST_HOST => 'translation.googleapis.com'; Readonly $REST_URL => "https://$REST_HOST/language/translate +/v2"; Readonly $CONSOLE_URL => "https://console.developers.google.com +/cloud-resource-manager"; Readonly %SIZE_LIMIT_FOR => ( translate => 2000, # google states 2K but observed results +vary detect => 2000, languages => 9999, # N/A ); }

Also, I want to build a central hash to store the data. Values would go in with Getopt::Long and end up in %self . This treatment creates a class of it. One thing I don't see is where the value of $class gets passed to the sub new:

sub new { my ( $class, $param_hr ) = @_; my %self = ( key => 0, format => 0, model => 0, prettyprint => 0, default_source => 0, default_target => 0, data_format => 'perl', timeout => 60, force_post => 0, rest_url => $REST_URL, agent => ( sprintf '%s/%s', __PACKAGE__, $VERSION ), cache_file => 0, headers => {}, );

Replies are listed 'Best First'.
Re^4: chunking up texts correctly for online translation
by bliako (Monsignor) on Jul 01, 2019 at 08:58 UTC

    Edit: 2 problems with this: 1) use My::Module; breaks things when the package is actually implemented in same file, so comment that out. 2) wrong order ot bless. The correct is bless $self, $class;, I use bless $self => $class; to be reminded of the correct order.

    package My::Module; sub new { my ( $class, $param_hr ) = @_; $param_hr = {} unless defined $param_hr; my $self = { # hashref or arrayref key => 0, format => 0, #... }; bless $self => $class; # now your hash is an object of class $clas +s. if( exists $param_hr->{'key'} ){ $self->key($param_hr->{'key'}) } else { warn "param 'key' is required."; return undef } return $self; # return the hash, sorry now blessed into a class in +stance, hallelujah } # get or set the key sub key { my $self = $_[0]; my $akey = $_[1]; # optional key print "you called key() method on object '$self'\n"; if( defined $akey ){ print "key() : changing key to '$akey'\n"; $self->{'key'} = $akey; } return $self->{'key'}; } 1; package main; # uncomment only if My::Module is in separate file: #use My::Module; # the notation module->new adds "module" as the first argument to the +new() params. # so here is how $class is set in your question, although YOU dont pas +s class, Perl does. my $mod = My::Module->new({ 'key' => 123, }); die unless defined $mod; # the same notation applies when calling the method # Perl passes the object ref ($self=$mod) to the method as a first par +am print "my key: ".$mod->key()."\n";

    Sometimes defining constants via Readonly or otherwise is useful

      Thx, bliako, this is definitely the tree I want to bark up. I have nearly syntactic perl now, but I think I'm missing the forest for the trees here:

      $ ./1.MY.translate.pl Can't locate My/Module.pm in @INC (you may need to install the My::Mod +ule module) (@INC contains: /home/bob/perl5/lib/perl5/x86_64-linux-gn +u-thread-multi /home/bob/perl5/lib/perl5 /etc/perl /usr/local/lib/x86 +_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_6 +4-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/per +l/5.26 /usr/share/perl/5.26 /usr/local/lib/site_perl /usr/lib/x86_64- +linux-gnu/perl-base) at ./1.MY.translate.pl line 23. BEGIN failed--compilation aborted at ./1.MY.translate.pl line 23. $ cat 1.MY.translate.pl #!/usr/bin/perl -w use 5.011; package My::Module; sub new { my ( $class, $param_hr ) = @_; $param_hr = {} unless defined $param_hr; my $self = { # hashref or arrayref key => 0, format => 0, }; bless $class, $self; # now your hash is an object of class $class +. if ( exists $param_hr->{'key'} ) { $self->key( $param_hr->{'key'} ) +} else { warn "param 'key' is required."; return undef } return $self; # return the hash, now blessed into a class instance, +hallelujah } 1; package main; use My::Module; # the notation module->new adds "module" as the first argument to the +new() params. # so here is how $class is set in your question, although YOU dont pas +s class, Perl does. my $mod = My::Module->new( { 'key' => 123, } ); die unless defined $mod; # the same notation applies when calling the method # Perl passes the object ref ($self=$mod) to the method as a first par +am print "my key: " . $mod->key() . "\n"; $

      Why does perl want to look elsewhere for package MY::Module ?

        Oops, if you implement a package on same file, you don't use it. I have edited out use My::Module; and also fixed a couple of typos. And also the most important part: it should be bless $self, $class . This always makes me remember the order bless $self => $class; I have edited the original post.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11102200]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-19 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found