You might be tempted to use CLASS data to store references, counts and global information for collections of objects.

A better solution to that issue is to define a new collection class that holds these counters, and global information. As new objects are created, they should be added to an instance of the collection class - or better, use a method in the collection class to create instances of the sub-objects - that way you can manage them collectively.

That said, here is updated code for your Shoe.pm that ignores the advice above, but allows you to implement the Boot class without any methods (disable the current methods).

### In Shoe.pm package Shoe; use strict; use warnings; # Shoes with holes in their soles. my %num_holey; sub new { my $class = shift; my $self = {}; # ... bless $self, $class; return $self; } # Instance method that happens to access a class variable. sub wear_out_sole { my $self = shift; $self->incr_num_holey() ; #$num_holey++; print "Sole on this " . ref($self) . " has been worn out.\n"; $self->how_many_holey(); } # Class methods. sub how_many_holey { my $self = shift; local $_; print "Currently, there are $num_holey{$_} holey " . "$_(s).\n" for keys %num_holey; } sub incr_num_holey { my $self = shift; $num_holey{ref($self)}++; } 1;
Update:
And here is a modified Shoetest.pl that includes a new ShoeRack class that uses the collection method indicated above.
#!/usr/bin/env perl use strict; use warnings; use Shoe; use Boot; {package Shoe_Rack; sub new{ my $class = shift; my $self = {}; # ... bless $self, $class; return $self; } sub Buy{ my $self = shift; my $TypeToBuy = shift or die "What type to buy ?\n"; $self->{COUNT}{$TypeToBuy}++; # Count whatever we buy my $newthing = $TypeToBuy->new(); push @{$self->{COLLECTION}}, $newthing; return $newthing; } sub getcollection{ my $self = shift; return @{$self->{COLLECTION}}; } 1; } my $s = Shoe->new(); $s->wear_out_sole(); my $b = Boot->new(); $b->wear_out_sole(); my $b2 = Boot->new(); $b2->wear_out_sole(); my $Rack = new Shoe_Rack; my $s1 =$Rack->Buy("Shoe"); $Rack->Buy("Shoe"); $Rack->Buy("Boot"); $Rack->Buy("Boot"); $Rack->Buy("Shoe"); $_->wear_out_sole() for $Rack->getcollection();

     "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom


In reply to Re: Still not getting it: hashref OO class variables by NetWallah
in thread Still not getting it: hashref OO class variables by bramble

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.