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:
my @functions; # list of all function names my @typedefs; # list of all typedef names my @variables; # list of all variables names my @constants; # list of all constants names my %defined_in; # maps a name to a source file name my %pattern; # maps a name to a pattern that can be used to # look-up the name in a source file. my %calls # maps a function name to a list of function names # that are called by that function.
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
my %program;
Where for instance
$program{"functions"}
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Baldly globaling were no-one globaled before.
by athomason (Curate) on Aug 27, 2000 at 01:17 UTC | |
|
Re: Baldly globaling were no-one globaled before.
by knight (Friar) on Aug 27, 2000 at 04:42 UTC | |
|
Re: Baldly globaling were no-one globaled before.
by ZZamboni (Curate) on Aug 27, 2000 at 05:28 UTC | |
|
RE: Baldly globaling were no-one globaled before.
by ZZamboni (Curate) on Aug 27, 2000 at 22:19 UTC | |
|
Re: Baldly globaling were no-one globaled before.
by gumpu (Friar) on Aug 28, 2000 at 17:39 UTC |