Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: chunking up texts correctly for online translation

by jwkrahn (Abbot)
on Jun 15, 2019 at 00:20 UTC ( [id://11101375]=note: print w/replies, xml ) Need Help??


in reply to chunking up texts correctly for online translation

my $ini_path = qw( /home/bob/Documents/html_template_data/3.values.ini + ); ... my $input_directory = qw( /home/bob/Documents/meditations/castaways/translate/data );

You are assigning a list to a scalar variable. This only works because there is only one element in the list. The correct way is to assign a scalar value to a scalar variable:

my $ini_path = '/home/bob/Documents/html_template_data/3.values.ini';

Or a list to a list:

my ( $ini_path ) = qw( /home/bob/Documents/html_template_data/3.values +.ini );
if ( $prompt1 eq ( "y" | "Y" ) ) { show_lang_codes(); }
$ perl -le'use Data::Dumper; my $x = ( "y" | "Y" ); print Dumper $x' $VAR1 = "y";

That code is only comparing $prompt1 to a lower case "y" but not to an upper case "Y". If you want to compare to both lower and upper case you can do the following:

if ( $prompt1 eq "y" || $prompt1 eq "Y" ) { show_lang_codes(); }

Or:

if ( lc $prompt1 eq "y" ) { show_lang_codes(); }

Or:

if ( $prompt1 =~ /\Ay\z/i ) { show_lang_codes(); }

Replies are listed 'Best First'.
Re^2: chunking up texts correctly for online translation
by bliako (Monsignor) on Jun 15, 2019 at 08:55 UTC

    qq looks more suitable in this case (for OP: it justs double-quotes the parameter and returns a scalar)

      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 => {}, );

        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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-23 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found