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

Jay Hannah jhannah at omnihotels.com
Mon Nov 7 11:41:20 PST 2005


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

Jay Hannah wrote: 


	Oops... Looks like posting to the mailing list doesn't work? I subscribed successfully, but then my message bounced... 

	Below is my message if you have a minute. 

	Thanks! 

	j 


	_____________________________________________ 
	From:   System Administrator  
	Sent:   Monday, October 24, 2005 2:23 PM 
	To:     'class-date at lists.dlux.hu' 
	Subject:        Undeliverable:Class::Date - change once set 

	Your message did not reach some or all of the intended recipients. 

	      Subject:  Class::Date - change once set 
	      Sent:     10/24/2005 2:22 PM 

	The following recipient(s) could not be reached: 

	      'class-date at lists.dlux.hu' on 10/24/2005 2:23 PM 
	            There was a SMTP communication problem with the recipient's email server.  Please contact your system administrator.

	            <exchange2k3.omnihotels.net #5.5.0 smtp;550 unknown user> 




	Hola -- 

	Is there any way to change a Class::Date value once one has been set? Right now my demo script is failing: 

	My script: 

	--- 
	use Class::Date qw( date ); 

	my $d1 = date "1970-01-01"; 
	my $d2 = date "2000-01-01"; 

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

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

	When I run it: 

	$ perl j.pl 
	[2000-01-01 00:00:00][2000-01-01 00:00:00] 
	[1970-01-01 00:00:00][2000-01-01 00:00:00] 

	$d1 is getting a NEW object, not overwriting the original $d1, so when stuff() returns I have lost my change to $d1. 

	Is there any way to change $d1 inside stuff()? I tried clone() and set() without any luck. 

	Thanks! 

	j 





-- 

Szabó Balázs (dLux)
--   --  - - - -- -


More information about the Omaha-pm mailing list