|
The reason that adding @truc changed the results is a bit
involved. If a class has no :InitArgs hash and no fields
with :Arg, then its :Init subroutine gets all initialization
parameters. Otherwise, the init params are filtered. (I'll
work on adding some explanation of this to the POD.)
Therefore, by adding @trun which has an :Arg attribute, you
need to also add a :InitArgs hash in order to get 'bidule':
#!/usr/bin/perl
use strict;
use warnings;
package Connection; {
use Object::InsideOut;
my @bidule
:Field
:Set(name => 'set_bidule', restricted => 1);
my @truc
:Field
:Arg(name => 'truc');
my %init_args :InitArgs = (
'bidule' => ''
);
sub init :Init
{
my ($self, $args) = @_;
# Set default, if needed
if (! exists($args->{'bidule'})) {
$args->{'bidule'} = 0;
}
# Store value
$self->set(\@bidule, $args->{'bidule'});
}
sub print_information
{
my $obj = shift;
print "Bidule: $bidule[$$obj]\n";
}
}
package Connection::Simulate; {
use Object::InsideOut('Connection');
sub pre :PreInit
{
my ($self, $args) = @_;
# Set default, if needed
if (! exists($args->{'bidule'})) {
$args->{'bidule'} = 1;
}
}
}
package main;
my $sim = Connection::Simulate->new();
$sim->print_information();
my $con = Connection->new();
$con->print_information();
print("Done\n");
exit(0);
# EOF
However, now that I've added the new :Default(...) attribute
in v3.04, you could use:
#!/usr/bin/perl
use strict;
use warnings;
package Connection; {
use Object::InsideOut;
my @bidule
:Field
:Def(0)
:Set(name => 'set_bidule', restricted => 1);
my @truc
:Field
:Arg(name => 'truc');
sub print_information
{
my $obj = shift;
print "Bidule: $bidule[$$obj]\n";
}
}
package Connection::Simulate; {
use Object::InsideOut('Connection');
sub init :Init
{
my ($self, $args) = @_;
$self->set_bidule(1);
}
}
package main;
my $sim = Connection::Simulate->new();
$sim->print_information();
my $con = Connection->new();
$con->print_information();
print("Done\n");
exit(0);
# EOF
|