[sf-perl] need help understanding perl

David Christensen dpchrist at holgerdanske.com
Sat Feb 6 13:28:30 PST 2021


On 2021-02-05 22:49, sneha rudravaram wrote:
> hello folks
> i am very new to perl. 

Welcome!  :-)


> i am trying to use the below code from CPAN.

Please provide a URL for the CPAN code.


Have you made any changes to the code?


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

That code is a fragment, and not a complete program.  It appears the 
subroutine 'reach' is doing a recursive depth-first search of the data 
structure referred to by the package lexical variable '$C', looking for 
the parameter '$ref', and returning various things depending upon the 
argument and what is found (or not found).


Please post the complete program or its URL.


Is there documentation?  If so, please post its URL.


> 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']]


For sample runs, please post complete console sessions -- prompt, 
command entered, output produced, etc..


> also, what are the concepts that are being used in this code? 

Subroutines -- definitions, context, arguments (outside), parameters 
(inside; @_), return values.

Expression evaluation.

Conditionals.

Variables -- singular (scalars), collections (arrays, hashes), 
references, allocation, initialization, assignment, discarding.

Recursion.


> are there any books/courses i can take for this?

I started with "Programming Perl" (2 e.?).  It is the canonical 
reference manual for Perl:

 
https://www.oreilly.com/library/view/programming-perl-4th/9781449321451/


I was baffled and discouraged.  Fortunately, there was "Learning Perl". 
I recall reading, typing in the examples, and doing the exercises for 
the first half of the book in under a day, and loving it.  I recommend 
that you start here:

     https://www.oreilly.com/library/view/learning-perl-7th/9781491954317/


I also found the "Perl Cookbook" to be very helpful:

    https://www.oreilly.com/library/view/perl-cookbook-2nd/0596003137/


I consider the above three books to be the canonical Perl texts.  These, 
plus the Internet, will allow you to understand many Perl programs and 
to write your own.


That said, there are many additional Perl books that I found to be 
useful.  "Intermediate Perl" would be very helpful for the above code:

    https://www.oreilly.com/library/view/intermediate-perl/0596102062/


A university education in computer science would be helpful; notably 
courses in data structures and algorithms.  I have not attended any Perl 
classes.


The Perl Beginner's mailing list was help when I was starting:

     https://lists.perl.org/list/beginners.html


David


More information about the SanFrancisco-pm mailing list