|
And I may not get it from the field, because it doesn't look like all the
initialized fields are set before Perl calls the ":init" subroutine.
No, the initialized fields are indeed set before the :Init subroutine is called. The following illustrates what I think you described:
#!/usr/bin/perl
use strict;
use warnings;
package My::Class; {
use Object::InsideOut;
my @birthyear_of :Field('Get' => 'year');
my @age_of :Field('Get' => 'age');
my %init_args :InitArgs = (
'birthyear' => {
'Field' => \@birthyear_of,
'Mandatory' => 1,
},
);
sub init :Init
{
my ($self, $args) = @_;
my $year = $birthyear_of[$$self];
my $age = (1900 + (localtime())[5]) - $year;
$age_of[$$self] = $age;
}
}
package main;
MAIN:
{
my $obj = My::Class->new('birthyear' => 1957);
print('Year: ', $obj->year(), "\n");
print('Age: ', $obj->age(), "\n");
}
exit(0);
# EOF
This produces the following output:
Year: 1957
Age: 48
|