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 2006-12-08 18:57:12-08 by jdhedden in response to 3735
Re: OIO and default value
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
Direct Responses: 3745 | Write a response