Welcome to the Monastery | |
PerlMonks |
Baldly globaling were no-one globaled before.by gumpu (Friar) |
on Aug 26, 2000 at 22:46 UTC ( [id://29826]=perlquestion: print w/replies, xml ) | Need Help?? |
gumpu has asked for the wisdom of the Perl Monks concerning the following question: Namaste I am pretty new to Perl (2 months). This is a question about globals and how to build elegant data structures. Say I have a Perl program that analyses a bunch of C source files. It figures out which functions, variables, constants, macros etc there are in the program and in which file they are defined. It also figures out which functions are called by which function and more useful information like that. After this is done it generates a number of HTML pages that show the structure of the program based on all this information. All the information has to be stored somewhere. One possibility is to use a bunch of globals, say:
Given this the temptation is great to write a couple of subroutines that fill these globals while scanning through the source code. With the subroutines taking just the names of the source files as parameter. Now we all know the mantra, "globals are evil and modifying globals in subs is even more evil". In a slightly more elegant method the subroutines take references to the various hashes and arrays that they modify. This has two disadvantages it seems:
Yet another option is to make another hash
Where for instance
returns a reference to the array with all function names, and $program{"defined_in"} returns a reference to the hash with file_names. All the subroutines can take a reference to this hash has parameter. This solves the long list of parameters problem. However the subroutines now have to cope with two levels of dereferencing. Is there a more elegant way to do this in Perl? Have fun
Back to
Seekers of Perl Wisdom
|
|