[Omaha.pm] Class::Date - change once set
Jay Hannah
jay at jays.net
Fri Nov 11 06:17:23 PST 2005
Forwarding dLux's message.
j
From: "Balázs Szabó (dLux)" <dlux at dlux.hu>
Date: November 11, 2005 3:06:01 AM CST
To: Jay Hannah <jay at jays.net>
Subject: Re: [Omaha.pm] FW: FW: Class::Date - change once set
Hi,
Jay Hannah wrote:
> From: "Balázs Szabó (dLux)" [mailto:dlux at dlux.hu]
>
>> use Class::Date qw(date);
>> $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";
>>
>> sub swap1 {
>> $x = $_[0];
>> $_[0] = $_[1];
>> $_[1] = $x;
>> }
>
>
> Ahhh, yes. I misread your email the first time. To make sure I know
> what's happening let me walk through it...
Ok.
>
>> sub swap1 {
>
>
> $a is obj in year 2000. $b is obj in year 1970. $_[0] is a ref to $a.
> $_[1] is a ref to $b.
Correct.
>> $x = $_[0];
>
>
> $x is created, a new obj in year 2000. (via clone() inside
> Class::Date)
Not correct. $_[0] is always points to the same object as $a, $_[1]
always points to the same object as $b; In this case, we have a new
object, $x, which is also points to the same as $a and $_[0];
>> $_[0] = $_[1];
>
> $a obj is destroyed. A new $a is created, year 1970. (via clone()
> inside Class::Date)
Not correct. Now $a is points to the original $b, while $x keeps the
reference to $a;
>> $_[1] = $x;
>> }
>
> $b obj is destroyed. A new $b is created, year 2000. (via clone()
> inside Class::Date)
>
> Is that right?
Not correct. $b now points to the $x, which kept the reference to the
original $a, and in the previous step, we saw that $a is now pointing to
the original $b;
>
> Thanks,
>
> j
>
So, it does not copy OBJECTS, it just increasing and decreasing
reference counters to objects (since perl is a reference-counting
language).
Please see the perlobj and perlref (or perlreftut) manual to get what I
had talked about.
Regards,
--
Szabó Balázs (dLux)
-- -- - - - -- -
More information about the Omaha-pm
mailing list