469,276 Members | 1,526 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,276 developers. It's quick & easy.

Inheritance

Hi,
I try to define an inheritance structure where each type of vehicle is in a separate module.Each module should give a simple print statement.Then the main package should print the details of each type of vehicle.So far I've got these two modules but after running the program can't locate Vehicle pm in @INC <INC contains:C:/Perl/lib C:/Perl/site/lib.How can I solve this problem?

Expand|Select|Wrap|Line Numbers
  1. package Vehicle;
  2. use warnings;
  3.  
  4. sub new {
  5.         my ($class) = @_;
  6.         my $self = {
  7.            _engine} => "I have an engine.",
  8.            _wheel   => "I have wheels.",
  9.            _door    => "I have doors." 
  10.         };
  11.         bless $self, $class;
  12.         return $self;    
  13. sub engine {
  14.     my ( $self, $engine ) = @_;  
  15.     $self->{_engine} = $engine;
  16.         return $self->{_engine};
  17. }
  18.  
  19. sub wheel {
  20.     my ( $self, $wheels ) = @_;
  21.         $self->{_wheel} = $wheel;
  22.     return $self->{_wheel};
  23. }
  24.  
  25. sub door {
  26.     my ( $self, $door ) = @_;
  27.         $self->{_door} = $door;
  28.     return $self->{_door};
  29. }
  30.  
  31. sub print {
  32.         my ($self) = @_;
  33.         print( "Motor vehicle\n\n\n",$self->engine, $self->wheel, $self->door );
  34. }
  35.  
  36. 1;  
  37.  
  38.  
  39. package Public;
  40. use Vehicle;
  41. use warnings;
  42. our @ISA = qw(Vehicle);
  43. sub new {
  44.         my ($class) = @_;
  45.         my $self = $class->SUPER::new();
  46.         $self->{_pub} = "I am public";
  47.         bless $self, $class;
  48.         return $self;    
  49. }
  50. sub pub {
  51.     my ( $self, $pub ) = @_;  
  52.     $self->{_pub} = $pub;
  53.         return $self->{_pub};
  54. }
  55.  
  56. sub print {
  57.         my ($self) = @_;
  58.         $self->SUPER::print;
  59. }
  60.  
  61. 1;  
This is the main package.

Expand|Select|Wrap|Line Numbers
  1. #!/perl/local/bin/perl
  2. use warnings;
  3. use Public::Vehicle;
  4.  
  5. my $vehicle = Vehicle->create("_engine", "_wheel", "_door");
  6.  
  7. print "Motor vehicle class:  ", $vehicle->_engine(), "\n";
  8. print "Motor vehicle class:  ", $vehicle->_wheel(), "\n";
  9. print "Motor vehicle class:  ", $vehicle->_door(), "\n";
Oct 9 '06 #1
1 1259
eWish
971 Expert 512MB
When you use the lib pragma you can tell it where to look for your modules, when they are not located in the typical location.

--Kevin
Nov 19 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Graham Banks | last post: by
4 posts views Thread by JKop | last post: by
5 posts views Thread by Morgan Cheng | last post: by
10 posts views Thread by davidrubin | last post: by
14 posts views Thread by Steve Jorgensen | last post: by
22 posts views Thread by Matthew Louden | last post: by
45 posts views Thread by Ben Blank | last post: by
6 posts views Thread by Bart Simpson | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.