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!

In reply to Array of Objects by mikeyYankoski

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.