doctor T has asked for the wisdom of the Perl Monks concerning the following question:

I have been trying to learn Perl...i was doing well then i tried to take a new step and things quit working. Now i have this code that i have been attempting to run on ideone.com. I know the problem starts on line 40ish (the first print statement)but i have not been able to figure out why the process crashes... It's a simple thing and looks very square because i am coming from a long C++ background. Any help that can get me off of this hump would be sooooo much appreciated.

package Employee; sub spawn { my $invocant = shift; my $class = ref($invocant) || $invocant; + my $self = { empid => 200, name => "Joe", pay => 20, @_, }; sub get_name { my $self = shift; return $self-> (name); } sub set_name { my $self = shift; $self -> (name) = shift; } sub get_pay { my $self = shift; return $self-> (pay); } sub set_pay { my $self = shift; $self -> (pay) = shift; } return bless $self, $class; } $wk1 = Employee->spawn(name=>"Joe", empid=>201, pay=>25); $wk2 = Employee->spawn(name=>"Moe"); $wk3 = Employee->spawn(name=>"Bob", empid=>202); print $wk1->name," Earns ",$wk1->pay," dollars per hour. His/her Emp I +D is: ",$wk1->empid; print $wk2->name," Earns ",$wk2->pay," dollars per hour. His/her Emp I +D is: ",$wk2->empid; print $wk3->name," Earns ",$wk3->pay," dollars per hour. His/her Emp I +D is: ",$wk3->empid; print "Got this far!"; $wk1->set_pay(70); $wk2->set_name("NewMoe"); print $wk1;#->name," Earns ",$wk1->pay," dollars per hour. His/her Emp + ID is: ",$wk1->empid; print $wk2;#->name," Earns ",$wk2->pay," dollars per hour. His/her Emp + ID is: ",$wk2->empid; print $wk3;#->name," Earns ",$wk3->pay," dollars per hour. His/her Emp + ID is: ",$wk3->empid;

Replies are listed 'Best First'.
Re: "Can't locate object method" error
by NetWallah (Canon) on Apr 18, 2017 at 19:22 UTC
    Multiple fixes and idiomatic optimizations:
    use strict; use warnings; {package Employee; sub spawn { my $invocant = shift; my $class = ref($invocant) || $invocant; + my $self = { empid => 200, name => "Joe", pay => 20, @_, }; return bless $self, $class; } sub empid { return $_[0]->{empid}}; sub name { my $self = shift; return $self-> {name}; } sub set_name { my $self = shift; $self-> {name} = shift; } sub pay { my $self = shift; return $self-> {pay}; } sub set_pay { my $self = shift; $self -> {pay} = shift; } sub PRINT{ my $self = shift; print $self->name," Earns ",$self->pay," dollars per hour. H +is/her Emp ID is: ",$self->empid, "\n"; } 1; # Indicates successful load when package is external } # End of package Employee my $wk1 = Employee->spawn(name=>"Joe", empid=>201, pay=>25); my $wk2 = Employee->spawn(name=>"Moe"); my $wk3 = Employee->spawn(name=>"Bob", empid=>202); for my $emp ($wk1,$wk2,$wk3){ $emp->PRINT(); } print "Got this far!", "\n"; $wk1->set_pay(70); $wk2->set_name("NewMoe"); for my $emp ($wk1,$wk2,$wk3){ $emp->PRINT(); }
    Your biggest problem was:
    return $self-> (name); # INCORRECT SYNTAX (looks like subroutine par +ameter) # vs. return $self-> {name}; # Braces indicate hash-element access

            ...Disinformation is not as good as datinformation.               Don't document the program; program the document.

      Thank you guys for the help. I can now move forward and attempt new and daring things. I might have figured out the problem with the print statement but adding that "package main" was a few days off... I will be back Dr t

Re: "Can't locate object method" error
by Anonymous Monk on Apr 18, 2017 at 18:59 UTC
    For your consideration:
    package Employee; sub spawn { my $class = shift; my $self = { empid => 200, name => "Joe", pay => 20, @_, }; return bless $self, $class; } sub get_name { my $self = shift; return $self->{name}; } sub set_name { my $self = shift; $self->{name} = shift; } sub get_pay { my $self = shift; return $self->{pay}; } sub set_pay { my $self = shift; $self->{pay} = shift; } package main; $wk1 = Employee->spawn(name=>"Joe", empid=>201, pay=>25); $wk2 = Employee->spawn(name=>"Moe"); $wk3 = Employee->spawn(name=>"Bob", empid=>202); print $wk1->get_name," Earns ",$wk1->get_pay," dollars per hour. His/h +er Emp ID is: ",$wk1->{empid}; print $wk2->get_name," Earns ",$wk2->get_pay," dollars per hour. His/h +er Emp ID is: ",$wk2->{empid}; print $wk3->get_name," Earns ",$wk3->get_pay," dollars per hour. His/h +er Emp ID is: ",$wk3->{empid}; print "Got this far!"; $wk1->set_pay(70); $wk2->set_name("NewMoe"); use Data::Dumper; print Dumper $wk1; print Dumper $wk2; print Dumper $wk3;
Re: "Can't locate object method" error
by karlgoethebier (Abbot) on Apr 19, 2017 at 08:50 UTC

    Consider Class::Tiny:

    #!/usr/bin/env perl use strict; use warnings; use feature qw(say); { package Employee; use Class::Tiny { name => 'Karl', empid => 311, pay => 200 }; say __PACKAGE__; } say __PACKAGE__; say for Class::Tiny->get_all_attributes_for("Employee"); my $wk = Employee->new(); say for ( $wk->name, $wk->empid, $wk->pay ); $wk->pay(3000); say $wk->pay; __END__

    See also Role::Tiny for further inspiration.

    Minor Update: Added __PACKAGE__

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    Furthermore I consider that Donald Trump must be impeached as soon as possible