[sf-perl] need help understanding perl

sneha rudravaram sneha.rudravaram at gmail.com
Fri Feb 5 22:49:53 PST 2021


hello folks
i am very new to perl. i am trying to use the below code from CPAN.

my $C;
# Recursive version of C<each>;sub reach {
    my $ref = shift;


    if (ref $ref eq 'HASH') {


        if (defined $C->{$ref}{v}) {
            if (ref $C->{$ref}{v} eq 'HASH') {
                if (my @rec = reach($C->{$ref}{v})) {
                    return ($C->{$ref}{k}, at rec);
                }
            } elsif (ref $C->{$ref}{v} eq 'ARRAY') {
                if (my @rec = reach($C->{$ref}{v})) {
                    if (defined $C->{$ref}{k}) {
                        return $C->{$ref}{k}, at rec;
                    }
                    return @rec;
                }

            }
            undef $C->{$ref};
        }


        if (my ($k,$v) = each %$ref) {
            $C->{$ref}{v} = $v;
            $C->{$ref}{k} = $k;
            return ($k,reach($v));
        }

        return ();


    } elsif (ref $ref eq 'ARRAY') {


        if (defined $C->{$ref}{v}) {
            if (ref $C->{$ref}{v} eq 'HASH' ||
                ref $C->{$ref}{v} eq 'ARRAY') {

                if (my @rec = reach($C->{$ref}{v})) {
                    if (defined $C->{$ref}{k}) {
                        return $C->{$ref}{k}, at rec;
                    }
                    return @rec;
                }
            }
        }



        if (my $v = $ref->[$C->{$ref}{i}++ || 0]) {
            $C->{$ref}{v} = $v;
            return (reach($v));
        }

        return ();
    }
    return $ref;
}

input: bar => {cmd_opts => { gld_upf => ['abc' , 'def']} } current
output: [bar, cmd_opts, gld_upf, abc]

[bar, cmd_opts, gld_upf, def]

desired output: [bar, cmd_opts, gld_upf, ['abc', 'def']]

also, what are the concepts that are being used in this code? are
there any books/courses i can take for this?

thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20210205/18e15b99/attachment-0001.html>


More information about the SanFrancisco-pm mailing list