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 ?