Perl Best Practices: Standards and Styles for Developing Maintainable Code

Category: Computer Science
Author: Damian Conway
All Stack Overflow 10


by anonymous   2017-08-20

[Edit I typed too slowly while answering, so this response bascially duplicates @mpapec's below - I will leave the references here and you can vote me up for those ;-) but do not accept my response as the answer].

Try something like the following to see if it works:

for $inner_hash (@{ $data1->{Family}{House} }) { 
   say "Name: $inner_hash->{Name}" 

since you need to get the inner hashes' values from inside the elements of the array (that is what value:ARRAY(OXXX) is telling you).

You can use perldoc to look at the perldata, perlref, perlreftut and perldsc PODs to learn more about data structures and dereferencing. If keeping your data structure in mind while you are writing code gets to be too hard to do, it may mean you need to simplify things: either the data itself or by writing sub's to make it easier to access, or making use some of the excellent utility modules from CPAN.

There's also some good perl data structure related tutorials out there. The POD/perldoc documentation that ships with perl (along with Chapter 9 of Programming Perl) is the canonical reference, but you might browse these nodes from perlmonks:

  • References quick reference
  • Referencing in advanced data structures
  • Visualizing perl data structures
  • Perlmonks Hash Tutorial

NB Above I'm using the perlcritic and Perl Best Practices style of dereferencing: e.g.: @{ $data1->{Family}{House} } so the syntax reminds me that the inner hashes (or inner-inner?) are inside an array. There's a cool new way of dereferencing being introduced in perl 5.20 called postfix dereferencing which will be great, but you can't go wrong following the recommendations of PBP.

"Until you start thinking in terms of hashes, you aren't really thinking in Perl." -- Larry Wall


by anonymous   2017-08-20

Well, first of all Higher-Order Perl is really good, but it's about functional programming, not objects.

Perl Best Practices is an excellent book, but it has limitations, and one of them is that Conway recommends using his own Class::Std module to do inside-out objects, and the general consensus seems to be (1) that if you're going to do inside-out objects, Object::InsideOut and Class::InsideOut are better ways to do it (2) and anyway, using "Moose" based objects is a better way to go.

This illustrates what is probably the major difference between the Java and Perl world: There's rarely one standard way of doing anything with Perl. Starting as someone who feels comfortable with objects in another language, I would guess that the most interesting thing about Conway's now slightly dated Object Oriented Perl is watching him gradually develop different ways of adding OOP features you've been taking for granted.

I like the basic blessed-hash style of Perl OOP myself, but you need to understand that it's encapsulation is really weak, and that while method-inheritance works, there's typically no data-inheritance. There are also some rather perlish tricks in wide use, such as automatically generating accessors using an AUTOLOAD routine.

As for what you can read on the subject, don't neglect the on-line documentation that comes with Perl: perldoc. Note the "Tutorials" section at the top. If you're rusty on Perl's references and data structures, read the first two: perldoc perlreftut and perldoc perldsc. A little down the list, you'll see multiple OOP tutorials. These are largely about simple href-based objects, though there are some serious oddities in there, such as Tom Christiansen's scheme for closure-based objects down at the bottom of: perldoc perltoot

If you're interested in some of the newer ways people do things, you might want to start with Moose which is supposed to be the closest you can get to perl6 objects while still writing perl5 code. By the way: ignore the word "postmodern" there, it's a silly joke that doesn't make any sense.

If you're interested in inside-out objects (which have bullet proof encapsulation, but are perhaps a little annoying to debug-- you can't just use Data::Dumper on the object to get it's status), I'd suggest starting with this perl5 wiki page.