$singleton->{xml_filename} = $_[0];
$singleton->{hostlist} = \@hostlist;
####
Dancer->new; # invocant is "Dancer"
$obj->dance; # invocant is $obj
####
my @components = @{ $config->{host}->{component} };
foreach (@components)
{
push @hostList, $_->{component_name};
}
####
push @hostList,
map { $_->{component_name} }
@{ $config->{host}->{component} };
####
{
package MyConfig;
use Moose;
use MooseX::Singleton;
use XML::Simple;
has xml_filename => (
is => 'ro',
isa => 'Str',
required => 1,
);
has _config => (
is => 'ro',
isa => 'Any',
lazy_build => 1,
);
has hostlist => (
is => 'ro',
isa => 'ArrayRef',
lazy_build => 1,
);
sub _build__config {
my $self = shift;
my $xml = XML::Simple->new;
$xml->XMLin($self->xml_filename, ForceArray => [ 'host', 'component' ])
or die $!;
}
sub _build_hostlist {
my $self = shift;
my @hostlist =
map { $_->{component_name} }
@{ $self->_config->{host}[0]{component} };
return \@hostlist;
}
}
my $config = MyConfig->new(xml_filename => "myconfig.xml");
my @hostlist = @{ $config->hostlist };
print "@hostlist";