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.


In reply to Re: "Can't locate object method" error by NetWallah
in thread "Can't locate object method" error by doctor T

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.