$ ./5.MY.translate.pl --configfile C --from tja --infile /home/bob/Documents/meditations/Algorithm-Markov-Multiorder-Learner-master/data/2.short.shelley.txt --outfile /home/bob/Desktop/1.state #### #!/usr/bin/perl -w use 5.011; binmode STDOUT, ":utf8"; use open IN => ':crlf'; use open OUT => ':utf8'; package My::Module; sub new { my ( $class, $param_hr ) = @_; $param_hr = {} unless defined $param_hr; my $self = { # hashref or arrayref key => 321, format => 5.23, START => time(), FROM => 'en', TO => 'ru', }; bless $self, $class; # now your hash is an object of class $class. if ( exists $param_hr->{'key'} ) { say "in new, param_hr is"; use Data::Dump; dd $param_hr; say "in new, self is "; dd $self; } if ( exists $param_hr->{'key'} ) { $self->key( $param_hr->{'key'} ) } else { warn "param 'key' is required."; return undef } return $self; # return hash, now blessed into a class instance, hallelujah } # get or set the key sub key { say "in sub key"; my $self = $_[0]; my $akey = $_[1]; # optional key say "akey is $akey "; 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; use Getopt::Long; my $outfile = undef; my $configfile = undef; my $infile = undef; my $from = undef; my $to = undef; if ( !Getopt::Long::GetOptions( "outfile=s", \$outfile, "infile=s", \$infile, "configfile=s", \$configfile, "from=s", \$from, "to=s", \$to, "help", sub { print "Usage : $0 --configfile C [--outfile O] [--infile I] [--help]\n"; exit 0; }, ) ) { die "error, commandline"; } die "configfile is needed (via --configfile)" unless defined $configfile; my $inFH; if ( defined($infile) ) { open( $inFH, '<:crlf:encoding(UTF-8)', $infile ) or die "opening input file $infile, $!"; } my $instr; { local $/ = undef; $instr = <$inFH> } close $inFH; if ( defined($instr) ) { say "input is $instr"; } say "----------------"; # uncomment only if My::Module is in separate file: #use My::Module; my $mod = My::Module->new( { 'key' => 123, 'CONTENT' => $instr, FROM => $from, TO => $to, } ); die unless defined $mod; print "my key: " . $mod->key() . "\n"; say "--- mod is"; dd $mod; __END__ #### 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 => {}, ); #### for my $property ( keys %self ) { if ( exists $param_hr->{$property} ) { my $type = ref $param_hr->{$property} || 'String'; my $expected_type = ref $self{$property} || 'String'; croak "$property should be a $expected_type" if $expected_type ne $type; $self{$property} = delete $param_hr->{$property}; } }