Defined a HashRef attribute in the lines of 'options' example.
has 'clsHsh' => (
traits => ['Hash'],
is => 'rw',
isa => 'HashRef[Str]',
default => sub {{}},
handles => {
set_clsHsh => 'set',
get_clsHsh => 'get',
has_no_clsHsh => 'is_empty',
num_clsHsh => 'count',
delete_clsHsh => 'delete',
clsHash_pairs => 'kv',
},
);
sub parseCls2()
{
my ($self,$line) = @_;
#input $line format is as follows
#$line = qq<Class att1=val1 att2=val3.../>)
#gather all key value pairs sans <Class
$line =~ s/<Class(.*)\/>/$1/g;
# split on space and take the resulting array
# split that on =. Simply equating to hash will convert
# key1,val1,key2,val2... to hash of key and value.
my %l4_sh = map {split /=/} split / /,$line;
$self->{clsHsh} = \%l4_sh;
return $self->{clsHsh};
}
#
# example of parsing a line
#
my $line = qq(<Class Cls="Bid" description="bid" low="" high=""/>)
;
my $h = $parser->parseCls2($line);
my %h1 = %$h;
while (my ($k,$v) = each %h1) {
say "key = $k and value = $v"
}
I am able to populate the clsHsh attribute using a sub that parses $line input parameter and stores the parsed values in the Hash. My questions,
- is there a way to repurpose set_clsHsh instead of using a sub?
- What is the recommended way?