|
The :PreInit subroutine should not try to set data in its
class's fields or in other class's fields (e.g., using 'set'
methods) as such changes will be overwritten during
initialization phase which follows preinitialization. The
:PreInit subroutine is only intended for modifying
initialization parameters prior to initialization.
(I will add the above to the POD for the next release.)
Here's the code that does what you want:
#!/usr/bin/perl
use strict;
use warnings;
package Connection; {
use Object::InsideOut;
my @bidule
:Field
:Arg(name => 'bidule', default => 0)
:Set(name => 'set_bidule', restricted => 1);
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();
print("Done\n");
exit(0);
# EOF
|