I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2007-01-25 10:41:01-08 by gaetan
Default value
Hi all,
I am using OIO version 3.05
When I start the following program
package toto; use strict; use warnings; use Object::InsideOut; my @attr :Field :Default({}) ; sub set_attr { my $obj = shift; my ($key, $value) = @_; $attr[$$obj]->{$key} = $value; } sub print { my $obj = shift; foreach my $p (keys %{$attr[$$obj]}){ print "$p => $attr[$$obj]->{$p}\n"; } } package main; my $t1 = toto->new(); $t1->set_attr('keyt1', 'valt1'); my $t2 = toto->new(); $t2->set_attr('keyt2', 'valt2'); $t2->print(); 1;
the output of the program is
bash>perl ~/tmp/toto.pl keyt1 => valt1 keyt2 => valt2

Is it normal ? I was waiting for the following output
bash>perl ~/tmp/toto.pl keyt2 => valt2
Is it a restriction ?
Thanks
Gaetan
Direct Responses: 4149 | Write a response
Posted on 2007-01-25 13:09:08-08 by jdhedden in response to 4148
Re: Default value
Using an array or hash reference as a default will probably not produce the result that you might expect:
my @foo :Field :Default({});
This does not result in a new empty hash reference being created for each new object. Rather, a single empty hash reference is created when the module is loaded, and then that reference is assigned to each newly created object. In other words, it is equivalent to:
my %bar = (); my @foo :Field :Default(\%bar);
To get the other result, assign a new empty hash reference using an :Init subroutine:
sub _init :Init { my ($self, $args) = @_; $foo[$$self] = {}; }
This explanation will be part of the POD in the next release. Thanks.
BTW, OIO 3.08 is the latest version.
Direct Responses: Write a response