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}; }

In reply to Re (tilly) 1: Array of Objects by tilly
in thread 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.