Could someone please explain this Perl method?

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 }->@*;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật