Creating objects in Perl

Reply

Join Date: Sep 2004
Posts: 55
Reputation: apcxpc is an unknown quantity at this point 
Solved Threads: 0
apcxpc's Avatar
apcxpc apcxpc is offline Offline
Junior Poster in Training

Creating objects in Perl

 
0
  #1
Oct 9th, 2005
Hi all

Sorry, I didn't know where to post my perl programming question.

I'm having a problem creating objects in perl. I found a piece of code on the internet, that gives an example of classes and objects in perl. I modified it a little bit, and now have this code:

  1. #!/usr/bin/perl
  2. use strict;
  3.  
  4. my $dog1 = new("Dog","Shane","white");
  5. my $dog2 = new("Dog","Tom","brown");
  6.  
  7. print "I am a dog called ". $dog1->getName()." and I am " . $dog1->getColour() . "\n";
  8. print "I am a dog called ". $dog2->getName()." and I am " . $dog2->getColour() . "\n";
  9.  
  10.  
  11. sub new {
  12. my $class = shift; # Determine the class for the oject to create
  13. my $name=shift;
  14. my $colour=shift;
  15. my $obj = {}; # Instantiate a generic empty object
  16. bless $obj, $class; # 'bless' this new generic object into the desired class
  17. $obj->_init($name,$colour); # Run the '_init()' sub for this class of object
  18. return $obj; # Return our newly blessed and loaded object
  19. }
  20.  
  21.  
  22. package Dog;
  23.  
  24. my $obj;
  25.  
  26. sub _init {
  27. $obj=shift;
  28. $obj->{NAME}=shift;
  29. $obj->{COLOUR} = shift;
  30. }
  31. sub getName{
  32. return $obj->{NAME};
  33. }
  34. sub getColour{
  35. return $obj->{COLOUR};
  36. }
The problem is that whenever I create an object, it sort of overwrites the old object. So the above code gives the output:

"I am a dog called Tom and I am brown."
"I am a dog called Tom and I am brown."


What am I doing wrong??
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Creating objects in Perl

 
0
  #2
Oct 9th, 2005
You shouldn't have $Dog::obj be a package global there. Make it a local variable in _init.

Your getColour, getName subroutines are returning the attributes of whatever hash reference is contained in $Dog::obj. This happens to point to the hash of whatever Dog was initialized most recently. They should return the attributes of their passed argument, @_[0], instead.

Perl packages do not work the same way as C++ classes.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 55
Reputation: apcxpc is an unknown quantity at this point 
Solved Threads: 0
apcxpc's Avatar
apcxpc apcxpc is offline Offline
Junior Poster in Training

Re: Creating objects in Perl

 
0
  #3
Oct 10th, 2005
Thanks for the help, Rashakil Fol.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC