-1

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,

  1. is there a way to repurpose set_clsHsh instead of using a sub?
  2. What is the recommended way?
  • _"repurpose set_clsHsh "_ what do you mean by "repurpose"? – Jim Garrison Dec 10 '21 at 00:17
  • I use sub parse($line) to populate the clsHsh. Can I instead use set_ClsHsh instead? if so, how? – user14797155 Dec 10 '21 at 01:47
  • Please edit your question. Add a complete runnable example. How does your `parse()` access clsHash? The only purpose of set_clsHsh is to set elements in this hash: `$obj->set_clsHsh($key,$val)` – clamp Dec 10 '21 at 09:47

1 Answers1

0

@clamp - thanks for the hint. I modified the sub in the Parse.pm file and it now works.

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.
            $self->set_clsHsh(map {split /=/} split / /,$line);

         }

Now, my access methods (parse_test.pl) to get clsHsh became simpler.

my $line = qq(<Class Cls="Bid" description="bid" low="low" high="high"/>)
;
$parser->parseCls2($line);
my $k1 = "low";
say $parser->get_clsHsh($k1);