Yes, that error is occurring because you did not declare a variable. But there is a bigger issue: you are trying to execute some code that should be in your client inside your object. Do this instead:

package Person; use strict; use warnings; sub new { my $class = shift; my $self = { _firstName => shift, _lastName => shift, _ssn => shift, }; # Print all the values just for clarification. print "First Name is $self->{_firstName}\n"; print "Last Name is $self->{_lastName}\n"; print "SSN is $self->{_ssn}\n"; bless $self, $class; return $self; } sub setFirstName { my ( $self, $firstName ) = @_; $self->{_firstName} = $firstName if defined($firstName); return $self->{_firstName}; } sub getFirstName { my( $self ) = @_; return $self->{_firstName}; } package Employee; use strict; use warnings; our @ISA = qw(Person); # inherits from Person package main; use strict; use warnings; use Data::Dumper; my $object = Person->new( "Thanos", "Test", 123456); print Dumper $object; $object = Employee->new( "Janus", "Employee", 998877); print Dumper $object;

Moose and alternatives can simplify the learning curve:

package Person; use Moose; use MooseX::FollowPBP; has firstName => ( is => 'rw', isa => 'Str', required => 1 ); has lastName => ( is => 'rw', isa => 'Str', required => 1 ); has ssn => ( is => 'ro', isa => 'Str' ); sub get_Name { join ' ', $_[0]->get_firstName, $_[0]->get_lastName } package Employee; use Moose; extends 'Person'; package main; use strict; use warnings; my @objects = ( Person->new( firstName => "Thanos", lastName => "Test", ssn => 123 +456), Employee->new( firstName => "Janus", lastName => "Employee", ssn = +> 998877), ); print $_->get_Name, $/ for @objects;

Finally, i can't vouch for that tutorial you linked to. Try perlbootut instead?

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re: Inheritance without defining the object on inherited module by jeffa
in thread Inheritance without defining the object on inherited module by thanos1983

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.