I have (at long last) hunkered down on my first OO project. I've read Damian's book, (even attended his class at O'Reilley's U. of Perl) and I think I have a reasonable good grasp of the basics. My project was humming along well until I hit a pretty weird snag, and I don't know what to do.

The project I'm working on generates an HTML-formatted monthly calendar (and before anyone suggests HTML::CalendarMonth on CPAN I've already looked at it... it's WAY more heavy weight than I need. My module is an order of magnitude smaller.) I wanted to give the user the ability to pass HTML formatting to individual table cells (make the cell for February 6th red, for example) so my first attempt was this:

sub format_cell { my ($self, $format, @days) = @_; # @days lets you apply formatting to more # than one date at a time. foreach (@days) { $self->{'cell_format'}[$_] = $format; } }
...where $format is a reference to a hash. (This way I get to pass HTML formatting directly to the HTML output functions in CGI.pm.) This works all fine and dandy, but since I'm doing straight assignment whatever formatting that's passed in completely replaces whatever formatting was there, so that if the formatting originally was {align => "left"} it gets clobbered when you pass {bgcolor => "red"}.

The answer is just to assign only to the keys I need instead of clobbering the whole hash, so here's try #2:

sub format_cell2 { my ($self, $format, @days) = @_; foreach (@days) { foreach my $key (keys %$format) { ${$self->{'cell_format'}[$_]}{$key} = + ${$format}{$key}; } } }
Same as before, only now I'm just referencing the hash keys passed, and only the ones passed change values in my object. I thought certainly this would work.

The problem is that whenever this method is called on a single object it changes ALL the values on ALL the days, and, as a particularly weird bonus, it changes it on EVERY object created with the module. (One object is created for each month... I have a program creating, say, 6 months at a time.)

Here's an actual example of how I call the method:

$html_months{$key} -> format_cell2({bgcolor => "red"}, 15)
...where $html_months{$key} contains an object for some month. The idea is that the cell for the 15th of that month should now be red, but in reality, ALL cells become red, on ALL objects in %html_months. This happens even if the method is called prior to the creation of the other objects.

I have a theory that I have somehow called a Class method, which would explain the universality of my problem, but in my debugging I've verified that it's only being called against a single object, and only one time.

Sigh... sorry for this being so long, but I really have no idea what to do. Any help from any OO Monks out there would be appreciated.

Gary Blackburn
Trained Killer


In reply to Strange method behavior by Trimbach

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.