Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^5: chunking up texts correctly for online translation

by Aldebaran (Curate)
on Jul 01, 2019 at 22:15 UTC ( [id://11102279]=note: print w/replies, xml ) Need Help??


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

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 ?

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

    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://11102279]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-26 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found