[sf-perl] need help understanding perl

al s deehun at gmail.com
Sat Feb 6 15:53:54 PST 2021


Some of the prior questions are answered by the post here:
https://stackoverflow.com/questions/66074268/need-to-modify-perl-deephashutils

So the code fragment comes from Deep::Hash::Utils. There are simpler ways
of doing what you need based on your single example, but the followup in
the above post makes a good point:

*Need to clarify what precisely you want -- in general, not only for the
given input example. To leave array-references as they are? Um ... all of
them? What if they contain hashrefs, like [ { a => 1 }, 'b' ]?*

Anyway, I wouldn't try to start learning perl by reading arcane CPAN code.
That's likely to end in disaster. Arcane library code of any language is
going to be hard to decipher for a beginner. But if you really want to
change the reach() code, just don't traverse the array ref (with the caveat
that this will also not traverse hash inside an array refs). If you want
better help, you'll need to be more specific and give a few more examples
of what you are trying to do.

On Sat, Feb 6, 2021 at 1:29 PM David Christensen <dpchrist at holgerdanske.com>
wrote:

> 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
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> https://mail.pm.org/mailman/listinfo/sanfrancisco-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20210206/af51d2a8/attachment-0001.html>


More information about the SanFrancisco-pm mailing list