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

Jay Hannah jhannah at omnihotels.com
Tue Nov 8 07:32:20 PST 2005


Forwarding message as dLux requested.

j


-----Original Message-----
From: "Balázs Szabó (dLux)" [mailto:dlux at dlux.hu] 
Sent: Monday, November 07, 2005 5:53 PM
To: Jay Hannah
Cc: omaha-pm at pm.org
Subject: Re: FW: Class::Date - change once set

Hi,

It does not break the encapsulation, you probably misunderstood someting.

I have checked the omaha-pm list, and this swap1 method uses the @_
array, which holds the function parameters, which are references to any
variables.

If you use swap1 for integers, it will work as well:

$a = 1;
$b = 1;

swap1($a,$b);

But since $a and $b are used by reference, swap1(1,2) won't work.

Check the perlsub manual page, you can see how this variable works.

So Class::Date objects are immutable, and I only copied references, and
it is not really Class::Date specific what you copy with swap1.

Please understand how the @_ variable work! (But before it, understand
what references are. :-) )

If you need help, just write!

Regards,

Balázs (dLux)

Jay Hannah wrote:

>Hi dLux. Thanks!!
>
>This is funny because I just got done lecturing someone not to break encapsulation on your class (Class::Date), and now your advice is to do just that. Strange world, isn't it?
>
>Thanks so much for your help! Sorry the spammers are irritating you. I feel your pain there.
>
>Take care,
>
>j
>Omaha Perl Mongers: http://omaha.pm.org
>
>
>
>-----Original Message-----
>From: "Balázs Szabó (dLux)" [mailto:dlux at dlux.hu] 
>Sent: Monday, October 31, 2005 7:04 AM
>To: Jay Hannah
>Subject: Re: FW: Class::Date - change once set	
>
>Hi!
>
>Sorry, I removed the email-list, because of the lots of spam, but probably I will resurrect if there is a need for this...
>
>No, you cannot change the object, it is by design. Class::Date objects are immutable. Use it as it would be a simple integer value.
>
>What you can do is to use references for them. For example the @_ array holds references for the variables, not the variables themselves, so the following is working:
>
>use Class::Date qw(date);
>sub swap1 {
>    $x = $_[0];
>    $_[0] = $_[1];
>    $_[1] = $x;
>}
>
>$a = date "2000-11-11";
>$b = date "1970-10-21";
>
>print "a: $a, b: $b\n";
>swap1($a, $b);
>print "a: $a, b: $b\n";
>
>Regards,
>
>dLux
>


More information about the Omaha-pm mailing list