Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Array of Objects

by mikeyYankoski (Initiate)
on Mar 12, 2001 at 11:23 UTC ( [id://63767]=perlquestion: print w/replies, xml ) Need Help??

mikeyYankoski has asked for the wisdom of the Perl Monks concerning the following question:

This is sample code that I created merely to test simple arrays of objects that I have created. Unfortunately, each time I push a new object onto the array (thus giving the array a new pointer to a newly created object) all other pointers in the array become identical to the last one. Here is the test code, below it is the test class.
#!/usr/bin/perl use Class2; $i = Class2->new(); print "Element1 Definition:\n"; $i->data("3.01.01", "Heloo"); print $i->getMe(), "\n"; $j = Class2->new(); print "Element2 Definition:\n"; $j->data("4.01.99", "Wow"); print $j->getMe(), "\n"; push (@stuff, $i); push (@stuff, $j); print "\nArray Contains:\n"; $tmp = @stuff[0]; $tmp->printClass(); $tmp_date = $tmp->getDate(); print "$tmp_date\n"; print "Element1: ", $tmp->getMe(), "\n"; $tmp = @stuff[1]; $tmp->printClass(); $tmp_date = $tmp->getDate(); print "$tmp_date\n"; print "Element2: ", $tmp->getMe(), "\n"; package Class2; sub new { $class = shift; my $self = {}; bless($self); return $self; } sub data { @_ == 3 or die 'usage: Class2->data( DATE, SUBJ )'; ($me, $date, $subj) = @_; print "The Date is: $date\n"; print "The Subject is: $subj\n"; } sub printClass { print "Your class type is $class\n"; print "The Date is: $date\n"; print "The Subject is: $subj\n"; } sub getDate { return $date; } sub getMe { return $me; } return 1;
Thanks!

Replies are listed 'Best First'.
Re (tilly) 1: Array of Objects
by tilly (Archbishop) on Mar 12, 2001 at 11:41 UTC
    Your problem is that all of your data has been stored as globals in Class2. What you need to do is store it all within your object, and then you can have multiple objects in your class without problems. Here is the code. I have made the following changes:
    1. I changed the formatting. Hanging indent and 2 space indent is my habit. Hanging vs not hanging doesn't really matter. However there is solid evidence that having an indent in the range 2-4 significantly improves comprehension.
    2. I added strict. This will catch many unintentional mistakes. Look at its documentation for more. And if you do need globals, see vars.
    3. I sprinkled my liberally through the module.
    4. I moved data into the object.
    5. I changed your code to use the two argument form of bless.
    Here it is:
    package Class2; use strict; sub new { my $class = shift; my $self = {}; bless($self, $class); return $self; } sub data { @_ == 3 or die 'usage: Class2->data( DATE, SUBJ )'; my $me = shift; $me->{date} = shift; $me->{subj} = shift; print "The Date is: $me->{date}\n"; print "The Subject is: $me->{subj}\n"; } sub printClass { my $self = shift; my $class = ref($self); print "Your class type is $class\n"; print "The Date is: $self->{date}\n"; print "The Subject is: $self->{subj}\n"; } sub getDate { (shift)->{date}; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://63767]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2024-04-23 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found