http://qs1969.pair.com?node_id=169779

Hail thee fellow brethren!

The term ‘data structures’ inherently implies nested structures built up from other simpler structures to tie in a certain relational bond. While I have seen a number of articles/books dedicated to the sole subject of proper data structures design principles and practices, I couldn’t full together enough resources related specifically to Perl language. Although, this material was useful in that it gave me a good grounding in the general theory that seems to permeate all languages and cultures, I still feel the need for discussing topics specific to Perl. For the way structures are built in Perl may differ substantially from other languages such as C, Java, or even Pascal.

Perl is a very liberal language when it comes to building complex data structures. It is much simpler to build nested HoH or AoH structures in Perl than C, for example. However, as with the rest of the language, this may be subject to severe abuse especially at the hands of an inapt developer. In a couple perl scripts that I had to either maintain or review, I have seen data structures that exhibited the symptoms of a cancerous tumor whereby they would expand uncontrollably at run-time gobbling all available process memory thereby sending the surrounding system into abyss. On the other hand, I had also come across some very clever data structures that bore much sense and thereby facilitated overall understanding of the script, it’s algorithms used, and data relationships (actually, predominant number of modules approved on CPAN may contain examples of proper data structures handling…).

To summarize my past (solitary) meditations, let me bring up an itemized list of benefits and disadvantages of complex data structures in Perl. Remember, whether these hold true in any given case largely depends on the qualifications of individual ‘hacker’.
The benefits of elaborate data structures:

  • Simplify algorithms operating on chunks of data.
  • Clarify data relationships.
  • Simply subroutine parameter passing (use a hash ref or hash in place of a list of arguments).
  • Increase overall code maintainability.
  • Reduce run-time memory requirements.
  • Dare to add more to this list? :)


Drawbacks of complex data structures:

  • Well, added complexity to deal with on top of already ‘complex’ (pun intended) language.
  • Memory requirements (both run-time and when storing in a static form on the hard drive etc).
  • Tendency of sloppy data structures to obscure program logic.
  • Dare to add more to this list? :)


Note: I would like to also include sample code and more ‘discussion’ on each listed item as time allows. Most likely, I’ll add a few in the follow up posts to keep this node within a reasonable size. By the way, your input will also be greatly appreciated! ;-)

_____________________
$"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=<a HREF="/index.pl?node=%22ps%20-e%20-o%20pid%20"> "," $2 </a>;`@$_ +`?{print"+ $1"}:{print"- $1"}&&`rm $1`; print$\;}