[Columbus-pm] weird datetime / push behavior
Shane Zatezalo
shane at lottadot.com
Sun Jun 20 06:05:09 PDT 2010
Like Devon said, your original script wasn't updating actual value of $d that was pushed onto your array:
#!/usr/local/bin/perl -w
use strict;
use DateTime;
use Data::Dumper;
my @days;
foreach my $i ( 1..7 )
{
my $d = DateTime->now;
$d->add( 'days' => $i);
push( @days, $d );
}
print Dumper \@days;
On May 18, 2010, at 4:17 PM, Jonathan Hogue wrote:
> ah, thanks. I was misunderstanding the meaning of this "$VAR1->[0]"
>
> I added this in my loop, and it seems to work. (just creating a new
> object from the old)
> $d = DateTime->from_object( object => $d );
>
> On Tue, May 18, 2010 at 4:07 PM, Smith,Devon <smithde at oclc.org> wrote:
>> It adds the fist and only DateTime object. Dumper is showing that all
>> successive additions are in fact references to that first object. You
>> might want to create a new object for each iteration, depending on
>> actual needs.
>>
>> /dev
>>
>> --
>> Devon Smith
>> Consulting Software Engineer
>> OCLC Research
>> http://www.oclc.org/research/people/smith.htm
>>
>>
>> -----Original Message-----
>> From: columbus-pm-bounces+smithde=oclc.org at pm.org
>> [mailto:columbus-pm-bounces+smithde=oclc.org at pm.org] On Behalf Of
>> Jonathan Hogue
>> Sent: Tuesday, May 18, 2010 3:52 PM
>> To: columbus-pm
>> Subject: [Columbus-pm] weird datetime / push behavior
>>
>> see this sample script.
>>
>> use strict;
>> use DateTime;
>> use Data::Dumper;
>>
>> my $d = DateTime->now;
>>
>> my @days;
>> foreach my $i ( 1..7 )
>> {
>> push( @days, $d );
>> $d->add( 'days' => 1);
>> }
>>
>> print Dumper \@days;
>>
>> It adds the first datetime object... but isn't doing what you expect
>> for the next iterations... what's going on here?
>>
>> $VAR1 = [
>> bless( {
>> .....
>> }, 'DateTime' ),
>> $VAR1->[0],
>> $VAR1->[0],
>> $VAR1->[0],
>> $VAR1->[0],
>> $VAR1->[0],
>> $VAR1->[0]
>> ];
>> _______________________________________________
>> Columbus-pm mailing list
>> http://columbus.pm.org/
>> Columbus-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/columbus-pm
>>
>>
>> _______________________________________________
>> Columbus-pm mailing list
>> http://columbus.pm.org/
>> Columbus-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/columbus-pm
>>
> _______________________________________________
> Columbus-pm mailing list
> http://columbus.pm.org/
> Columbus-pm at pm.org
> http://mail.pm.org/mailman/listinfo/columbus-pm
More information about the Columbus-pm
mailing list