(I'm not all that versed on Object Oriented Programming so bear with
me...)<br/>
<br/>
i thought the whold reason for the use of "my" when declaring
variables was to limit their scope and keep from overlapping with other
delcarations of the same variable name.<br/>
<br/>
Hence, the parent scope ('my $d1 =
Class::Date->new("1971-01-01");') were protected from the
subroutine scope declaration of d1 and d2.<br/>
<br/>
Doesn't the "our" declaration do what you want? From the
"man perlfunc":<br/>
<br/>
<dl><dt>our EXPR</dt><dt><a name="our"></a><a
name="our_EXPR_TYPE"></a></dt><dd>
<p>An <code>our</code> declares the listed variables to be valid globals
within the enclosing block, file, or <code>eval</code>. That is, it has
the same scoping rules as a "my" declaration, but does not
create a local variable. If more than one value is listed, the list must
be placed in parentheses. The <code>our</code> declaration has no semantic
effect unless "use strict vars" is in effect, in which case it
lets you use the declared global variable without qualifying it with a
package name. (But only within the lexical scope of the <code>our</code>
declaration. In this it differs from "use vars", which is
package scoped.)</p>
</dd></dl><br/>
Here is what I came up with:<br/>
<pre><font size="2">#!/usr/bin/perl -w</font></pre>
<pre><font size="2">use Class::Date qw( date );</font></pre>
<pre><font size="2"><br/>
our $d1 = date "1970-01-01";</font></pre>
<pre><font size="2">our $d2 = date "2000-01-01";</font></pre>
<pre><font size="2"><br/>
print "Before stuff: [$d1][$d2]\n";</font></pre>
<pre><font size="2">stuff($d1, $d2);</font></pre>
<pre><font size="2">print "After stuff :
[$d1][$d2]\n";</font></pre>
<pre><font size="2"><br/>
sub stuff {</font></pre>
<pre><font size="2"> our ($d1,
$d2) = @_;</font></pre>
<pre><font size="2"> if ($d2
> $d1) {</font></pre>
<pre><font
size="2">
$d1 = $d2; # <---- I want to overwrite the existing $d1
here</font></pre>
<pre><font size="2"> }</font></pre>
<pre><font size="2"> print
"Inside stuff: [$d1][$d2]\n";</font></pre>
<pre><font size="2">}</font></pre>
<br/>
Dan<br/>
<br/>
On Mon, October 24, 2005 15:25, Kenneth Thompson wrote:<br/>
> Well, it's clumsy but it works. Maybe it could be written into the
class<br/>
> as another clone option?<br/>
> <br/>
> use Class::Date qw( date );<br/>
> <br/>
> my $d1 = Class::Date->new("1971-01-01");<br/>
> my $d2 = Class::Date->new("2000-01-01");<br/>
> <br/>
> print "[$d1][$d2]\n";<br/>
> stuff($d1, $d2);<br/>
> print "[$d1][$d2]\n";<br/>
> <br/>
> sub stuff {<br/>
> my ($d1, $d2) = @_;<br/>
> if ($d2 > $d1) {<br/>
> # $d1 = $d2; # <---- I want to overwrite the existing $d1
here<br/>
> foreach my $idx (0 .. 9) {<br/>
> $d1->[$idx] = $d2->[$idx];<br/>
> }<br/>
> }<br/>
> }<br/>
> <br/>
> <br/>
> -----Original Message-----<br/>
><br/>
From: omaha-pm-bounces@pm.org [mailto:omaha-pm-bounces@pm.org] On Behalf<br/>
> Of Jay Hannah<br/>
> Sent: Monday, October 24, 2005 2:22 PM<br/>
> To: class-date@lists.dlux.hu<br/>
> Cc: omaha-pm@pm.org<br/>
> Subject: [Omaha.pm] Class::Date - change once set<br/>
> <br/>
> <br/>
> Hola --<br/>
> <br/>
> Is there any way to change a Class::Date value once one has been
set?<br/>
> Right now my demo script is failing:<br/>
> <br/>
> My script:<br/>
> <br/>
> ---<br/>
> use Class::Date qw( date );<br/>
> <br/>
> my $d1 = date "1970-01-01";<br/>
> my $d2 = date "2000-01-01";<br/>
> <br/>
> stuff($d1, $d2);<br/>
> print "[$d1][$d2]\n";<br/>
> <br/>
> sub stuff {<br/>
> my ($d1, $d2) = @_;<br/>
> if ($d2 > $d1) {<br/>
> $d1 = $d2; # <---- I want to overwrite the existing $d1
here<br/>
> }<br/>
> print "[$d1][$d2]\n";<br/>
> }<br/>
> ---<br/>
> <br/>
> When I run it:<br/>
> <br/>
> $ perl j.pl<br/>
> [2000-01-01 00:00:00][2000-01-01 00:00:00]<br/>
> [1970-01-01 00:00:00][2000-01-01 00:00:00]<br/>
> <br/>
> $d1 is getting a NEW object, not overwriting the original $d1, so
when<br/>
> stuff() returns I have lost my change to $d1.<br/>
> <br/>
> Is there any way to change $d1 inside stuff()? I tried clone() and
set()<br/>
> without any luck.<br/>
> <br/>
> Thanks!<br/>
> <br/>
> j<br/>
> <br/>
> <br/>
> <br/>
> _______________________________________________<br/>
> Omaha-pm mailing list<br/>
> Omaha-pm@pm.org<br/>
> http://mail.pm.org/mailman/listinfo/omaha-pm<br/>
> <br/>
> _______________________________________________<br/>
> Omaha-pm mailing list<br/>
> Omaha-pm@pm.org<br/>
> http://mail.pm.org/mailman/listinfo/omaha-pm<br/>
> <br/>
<br/>
<br/>
- - - -<br/>
"Wait for that wisest of all counselors, time." -- Pericles<br/>
"I do not fear computer, I fear the lack of them." -- Isaac
Asimov<br/>
GPG fingerprint:6FFD DB94 7B96 0FD8 EADF 2EE0 B2B0 CC47 4FDE 9B68