convenientstore has asked for the wisdom of the Perl Monks concerning the following question:
and I was delighted as I was actually able to follow on very basics of the subject(for now, I just want to learn enough so that I can actually use OO modules in some scripts).. However, at the end of the article, I simply copy and paste the script and modules in hope to see the results but to my suprises, I get " cannot find the module Address.pm" Looking back at Person.pm, I see the use Address; statement. I am not sure if writer forgot to include a Address.pm or if this is standard modules that my perl is missing or?http://www.codeproject.com/perl/camel_poop.asp
PACKAGE EMPLOYEE#class Person package Person; use strict; use Address; #Person class will contain an Address #constructor sub new { my ($class) = @_; my $self = { _firstName => undef, _lastName => undef, _ssn => undef, _address => undef }; bless $self, $class; return $self; } #accessor method for Person first name sub firstName { my ( $self, $firstName ) = @_; $self->{_firstName} = $firstName if defined($firstName); return $self->{_firstName}; } #accessor method for Person last name sub lastName { my ( $self, $lastName ) = @_; $self->{_lastName} = $lastName if defined($lastName); return $self->{_lastName}; } #accessor method for Person address sub address { my ( $self, $address ) = @_; $self->{_address} = $address if defined($address); return $self->{_address}; } #accessor method for Person social security number sub ssn { my ( $self, $ssn ) = @_; $self->{_ssn} = $ssn if defined($ssn); return $self->{_ssn}; } sub print { my ($self) = @_; #print Person info printf( "Name:%s %s\n\n", $self->firstName, $self->lastName ); } 1;
MAIN PROGRAM# class Employee package Employee; use Person; use strict; our @ISA = qw(Person); # inherits from Person #constructor sub new { my ($class) = @_; #call the constructor of the parent class, Person. my $self = $class->SUPER::new(); $self->{_id} = undef; $self->{_title} = undef; bless $self, $class; return $self; } #accessor method for id sub id { my ( $self, $id ) = @_; $self->{_id} = $id if defined($id); return ( $self->{_id} ); } #accessor method for title sub title { my ( $self, $title ) = @_; $self->{_title} = $title if defined($title); return ( $self->{_title} ); } sub print { my ($self) = @_; # we will call the print method of the parent class $self->SUPER::print; $self->address->print; } 1;
use strict; use warnings; use diagnostics; use Employee; #create Employee class instance my $khurt = eval { new Employee(); } or die ($@); #set object attributes $khurt->firstName('Khurt'); $khurt->lastName('Williams'); $khurt->id(1001); $khurt->title('Executive Director'); $khurt->address( new Address() ); $khurt->address->street('10 Anywhere Lane'); $khurt->address->city('Anytown'); $khurt->address->state('NJ'); $khurt->address->zip('12345'); #diplay Employee info $khurt->print();
Can't locate Address.pm in @INC (@INC contains: /script/perl/test2/mod +ules /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/ +perl5/site_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/site_per +l/5.8.6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-l +inux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_ +perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5. +8.5 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-li +nux-thread-multi /usr/lib/perl5/vendor_perl/5.8.7/i386-linux-thread-m +ulti /usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi /usr/li +b/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vend +or_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_ +perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_per +l /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 . +) at /script/perl/test2/modules/Person.pm line 4. BEGIN failed--compilation aborted at /script/perl/test2/modules/Person +.pm line 4. Compilation failed in require at /script/perl/test2/modules/Employee.p +m line 3. BEGIN failed--compilation aborted at /script/perl/test2/modules/Employ +ee.pm line 3. Compilation failed in require at ./first.obj line 6. BEGIN failed--compilation aborted at ./first.obj line 6 (#1) (F) You said to do (or require, or use) a file that couldn't be found. Perl looks for the file in all the locations mentioned in @ +INC, unless the file name included the full path to the file. Perhaps +you need to set the PERL5LIB or PERL5OPT environment variable to say w +here the extra library is, or maybe the script needs to add the library + name to @INC. Or maybe you just misspelled the name of the file. See perlfunc/require and lib. Uncaught exception from user code: Can't locate Address.pm in @INC (@INC contains: /script/perl/t +est2/modules /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi / +usr/lib/perl5/site_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/ +site_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8. +5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/per +l5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site +_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8 +/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.7/i386-linux- +thread-multi /usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi + /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/pe +rl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5 +/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/ve +ndor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5 +/5.8.8 .) at /script/perl/test2/modules/Person.pm line 4. BEGIN failed--compilation aborted at /script/perl/test2/modules/Person +.pm line 4. Compilation failed in require at /script/perl/test2/modules/Employee.p +m line 3. BEGIN failed--compilation aborted at /script/perl/test2/modules/Employ +ee.pm line 3. Compilation failed in require at ./first.obj line 6. BEGIN failed--compilation aborted at ./first.obj line 6. at ./first.obj line 6
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: object oriented tutorial question
by jdporter (Paladin) on Sep 03, 2007 at 02:24 UTC | |
by brent.allsop (Acolyte) on Sep 03, 2007 at 02:53 UTC | |
Re: object oriented tutorial question
by cdarke (Prior) on Sep 03, 2007 at 11:10 UTC | |
by cdarke (Prior) on Sep 03, 2007 at 11:24 UTC | |
by Anno (Deacon) on Sep 04, 2007 at 00:36 UTC | |
by cdarke (Prior) on Sep 04, 2007 at 10:54 UTC | |
Re: object oriented tutorial question
by blazar (Canon) on Sep 03, 2007 at 15:19 UTC | |
Re: object oriented tutorial question
by FunkyMonk (Chancellor) on Sep 03, 2007 at 16:10 UTC |