944,123 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 8453
  • Perl RSS
Oct 9th, 2005
0

Creating objects in Perl

Expand Post »
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:

Quote ...
Perl Syntax (Toggle Plain Text)
  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??
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
Oct 9th, 2005
0

Re: Creating objects in Perl

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.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Oct 10th, 2005
0

Re: Creating objects in Perl

Thanks for the help, Rashakil Fol.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: problem with incrementing numbers in a file
Next Thread in Perl Forum Timeline: Perl Script Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC