[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



(I'm not all that versed on Object Oriented Programming so bear with me...)

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.

Hence, the parent scope ('my $d1 = Class::Date->new("1971-01-01");') were protected from the subroutine scope declaration of d1 and d2.

Doesn't the "our" declaration do what you want?  From the "man perlfunc":

our EXPR

An our declares the listed variables to be valid globals within the enclosing block, file, or eval. 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 our 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 our declaration. In this it differs from "use vars", which is package scoped.)


Here is what I came up with:
#!/usr/bin/perl -w
use Class::Date qw( date );

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

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

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

Dan

On Mon, October 24, 2005 15:25, Kenneth Thompson wrote:
> Well, it's clumsy but it works. Maybe it could be written into the class
> as another clone option?
>
> use Class::Date qw( date );
>
> my $d1 = Class::Date->new("1971-01-01");
> my $d2 = Class::Date->new("2000-01-01");
>
> print "[$d1][$d2]\n";
> 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
> foreach my $idx (0 .. 9) {
> $d1->[$idx] = $d2->[$idx];
> }
> }
> }
>
>
> -----Original Message-----
>
From: omaha-pm-bounces@pm.org [mailto:omaha-pm-bounces@pm.org] On Behalf
> Of Jay Hannah
> Sent: Monday, October 24, 2005 2:22 PM
> To: class-date@lists.dlux.hu
> Cc: omaha-pm@pm.org
> Subject: [Omaha.pm] Class::Date - change once set
>
>
> 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
>
>
>
> _______________________________________________
> Omaha-pm mailing list
> Omaha-pm@pm.org
> http://mail.pm.org/mailman/listinfo/omaha-pm
>
> _______________________________________________
> Omaha-pm mailing list
> Omaha-pm@pm.org
> http://mail.pm.org/mailman/listinfo/omaha-pm
>


- - - -
"Wait for that wisest of all counselors, time." -- Pericles
"I do not fear computer, I fear the lack of them." -- Isaac Asimov
GPG fingerprint:6FFD DB94 7B96 0FD8 EADF 2EE0 B2B0 CC47 4FDE 9B68