use warnings;
use strict;
package Factory {
sub new {
my ($class,%args) = @_;
my $self = {
config => _read_config( $args{config_file} ),
};
return bless $self, $class;
}
sub _read_config {
my $file = shift;
my %config;
# ...
return \%config;
}
sub make_object_from_data {
my ($self,$data) = @_;
my %args;
# complex code to build %args from $data using $self->{config}
return Object->new(%args);
}
}
package Object {
sub new {
my ($class,%args) = @_;
my $self = \%args;
return bless $self, $class;
}
# ...
}
my $factory = Factory->new( config_file => "file.xml" );
my $obj1 = $factory->make_object_from_data( { foo=>123 } );
my $obj2 = $factory->make_object_from_data( { bar=>456 } );
####
use warnings;
use strict;
package Config {
sub new {
my ($class,%args) = @_;
my $self = {};
# code to read $args{config_file} into $self
return bless $self, $class;
}
}
package Object {
sub new {
my ($class,%args) = @_;
my $self = _parse_data_with_config($args{config},$args{data});
return bless $self, $class;
}
sub _parse_data_with_config {
my ($config, $data) = @_;
my $self = {};
# complex code to build $self from $data using $config
return $self;
}
# ...
}
my $config = Config->new( config_file => "file.xml" );
my $obj1 = Object->new( config => $config, data => { foo=>123 } );
my $obj2 = Object->new( config => $config, data => { bar=>456 } );
####
sub new_object {
my ($self,$data) = @_;
return Object->new( config=>$self, data=>$data );
}
# ... call as:
my $obj3 = $config->new_object( { quz=>789 } );