|
Normally, object fields are declared as part of the class's code. However, some classes may require the creation of additionaly object fields as the application runs, especially in conjunction with :Automethods. This release of Object::InsideOut provides a class method that allows class code to dynamically create object fields. Here's an elaborate example used in conjunction with :Automethod
package My::Class; {
use Object::InsideOut;
sub auto :Automethod
{
my $self = $_[0];
my $class = ref($self) || $self;
my $method = $_;
# Extract desired field name from get_/set_ method name
my ($fld_name) = $method =~ /^[gs]et_(.*)$/;
if (! $fld_name) {
return; # Not a recognized method
}
# Create the field and its standard accessors
Object::InsideOut->create_field($class, '@'.$fld_name,
"'Standard'=>'$fld_name'");
# Return code ref for newly created accessor
no strict 'refs';
return *{$class.'::'.$method}{'CODE'};
}
}
|