[Omaha.pm] Class::Date - change once set

Kenneth Thompson kthompson at omnihotels.com
Wed Oct 26 07:32:50 PDT 2005


Actually, I'm not so sure this is as bad a thing at it seems on the
surface. The perldoc states that it 
exposes the internal pieces via an array of parts/formats:

-------------
INTERNALS
This module uses operator overloading very heavily. I've found it quite
stable, but I am afraid of it a bit.
A Class::Date object is an array reference.
-------------

Given that the array has been exposed and documented on purpose, I
wouldn't think that this type of manipulation would be bad. Even if they
change the array, it's setting one object's internals equal to the other
objects internals. Is that the same as accessing the internals for
manipulation? Of course the hard count reference (0..9) should be a
little more dynamic in case the array size is increased for some reason.

However- what about a whole different approach? Maybe a referenced
workaround?

use Class::Date qw( date );
#Clone a date object into and existing date object

my $d1 = Class::Date->new("1971-01-01");
my $d1R = \$d1;
my $d2 = Class::Date->new("2000-01-01");

print "[".$d1."][$d2]\n";
stuff($d1, $d2);$d1 = $$d1R;
print "[$d1][$d2]\n";


sub stuff {
    my ($d1, $d2) = @_;
    if ($d2 > $d1) {
      $d1R = \$d2;   # <---- I want to overwrite the existing $d1 here
    }
}

And for discussion- why 

    my ($d1, $d2) = @_;

and not 

    ($d1, $d2) = @_;

? (by the way... The second way works the way you wanted it to) 


-----Original Message-----
From: omaha-pm-bounces at pm.org [mailto:omaha-pm-bounces at pm.org] On Behalf
Of Jay Hannah
Sent: Tuesday, October 25, 2005 9:55 PM
To: Perl Mongers of Omaha, Nebraska USA
Subject: Re: [Omaha.pm] Class::Date - change once set

On Oct 24, 2005, at 3:25 PM, Kenneth Thompson wrote:
> sub stuff {
>     my ($d1, $d2) = @_;
>     if ($d2 > $d1) {
> #      $d1 = $d2;   # <---- I want to overwrite the existing $d1 here
>        foreach my $idx (0 .. 9) {
>          $d1->[$idx] = $d2->[$idx];
>        }
>     }
> }

Yikes! Break encapsulation and jack the object's internals? I'll be
doomed if the internal implementation of Class::Date ever changes...

j


_______________________________________________
Omaha-pm mailing list
Omaha-pm at pm.org
http://mail.pm.org/mailman/listinfo/omaha-pm



More information about the Omaha-pm mailing list