Unfortunately, I have inherited a very large Perl codebase and I need to do some modifications to one of its features.
The program is reading a CSV file and does some cleanup on it to create a protobuf message. There is a key value pair foo:bar
in a temp_params{}
part of the input string.
I managed to trace where I think the temp_params are getting parsed. For now, please ignore dp, let’s assume that dp will always be empty. The Key-value I’m expecting is part of tp, not dp.
sub _add_temp_and_dynamic_params {
my ($self, $row) = @_;
state $raw_to_full = {
tp => 'temp_params',
dp => 'dynamic_params',
};
while ( my ( $raw, $full ) = each %$raw_to_full ) {
my @params =
map { { key => $_->[0], value => $_->[1] } }
grep { @$_ == 2 && $_->[0] ne '' }
map { [ split ":" ] }
split /,/, ( delete $row->{ $raw } or next );
$row->{ $full }->@* = @params if @params;
}
}
After this, I want to call _my_new_subroutine
with the output of that method above, and the goal of that method is to take that temp_params{foo:bar}
so that foo:bar
ends up as its own independent key-value pair
The problem is: I really have no idea what is going on in this method or how the output looks like. This looks extremely cryptic to me. I don’t know how raw_to_full
could contain any meaningful info, since it seems that it’s just creating a hash where the key is tp
and the value is the string 'temp_params'
. I’m not even sure if my method will need to work with a hash or with a string. What does ( delete $row->{ $raw } or next )
even mean? or $row->{ $full }->@* = @params if @params;
? I heard many times that Perl had the ill-fame of being a “write-only language”, but I had no idea it would be this bad.
So far, what I understand is:
Method assigns the $self variable because perl methods always pass their own caller as an argument. Then $row is added. The $ indicates scalar, and this is operating on csv lines, so I assume that the input is a string.
Then, a local variable $raw_to_full is being declared, this is a hash where tp and dp are keys, and strings ‘temp_params’ and ‘dynamic_params’ are their respective values. (This also leads to another question: if this is a hash, why is this being indicated with a $ instead of a %?)
Then, 2 new local variables are created: raw and full, which are the result of “Spliting” the key and the values in each entry in the raw_to_full hash.
For every iteration, a string is created where the word ‘key’ is actually declared as key itself, and the value of key is the ‘raw’ value whereas ‘full’ is the value (not so sure about this.
Then a filter is done to only include rows with key-value, where the value is not empty.
Then, a split is created at where ‘:’ is indicated.
Lastly, another split is done to separate I assume the different kv pairs inside of each “params” string.
After that, I’m completely lost as to what could be happening here.
4
Use a dumping tool too look at your data structure. You’ll find you get
$row->{ temp_params } = [
{ key => "foo", value => "bar" },
];
This format makes it hard to access params by key, but it allows multiple params with the same key.
To find the values for a key, you will need to iterate over the array.
my @values_for_foo =
map { $_->{ value } }
grep { $_->{ key } eq "foo" }
$row->{ temp_params }->@*;
That said, you only keep the last value for any given key. (So why use this structure?!?) To get the only value for a key (or undef
if not found),
my ( $value_for_foo ) =
map { $_->{ value } }
grep { $_->{ key } eq "foo" }
$row->{ temp_params }->@*;