Lance wrote:

> If I hafta pass in refs from loop1 2 and 3 all the way down the line, So Be
> It.  It just makes my argument list a little unsightly, is all.

Hi Lance,

You might want to re-examine this view.  The process of passing by reference is not 
something to just resign yourself to.  It is the key to powerful programming.  As long 
as your code depends on external values, such as global variables, you will be limited 
in the scale of operations you can handle.

I don't particularly enjoy the way Perl handles references.  I prefer the C++ feature 
which allows one to simply declare a parameter as a reference, and use it afterwards 
trrasnsparently--as if it was the original.  Nevertheless, with a little efort, you 
can get the Perl reference paradigm down, and it does have an internal consistency, at 
least.

ook at it this way--on a 50-100 line quick-and-dirty job, global variables seem to be 
convenient.  You see a variable like $counter, and of course you remember what value 
it is keeping track of.  Sure, on a 100-line script, that works fine.  What about when 
your code is part of a system encompassing tens or hundreds of thousands of lines.?  
Do you really want to count on knowing what some variable declared 500 lines back, or 
in a completely different module of your project, means, or count on being able to 
access it without interfering with some other project?

I would suggest as a wothwhile goal designing each subroutine so that it is a 
self-contained package.  Explicitly declaring each value accessed within a sub imposes 
a healthy discipline.  It can also help you prevent confusion.

# Loads the hash referred to by the first parameter with name=value pairs stored inthe 
file named in
#  the second parameter.  File format for each line is:
# Name=Value [with spaces allowed]
# All '=' in name or value must be escaped as hex
sub LoadHashFromFile {
    my ($contents, $source) = @_;
   open SOURCE, $source or die "Can not open $source $!\n";
   while (<SOURCE>) {
      chomp;
      my ($name, $value) = split (/=/, $_);
      $name =~  /%3d/=/;
      $value =~ /%3d/=/;                      #restore any escaped equals signs in 
value
      $$contents{$name} = $value;        #  see perlref & perlreftut for explanation 
of $$ dereference
   }
   close SOURCE or die "Could not close $source properly. $!\n";
}

Can you see the advantage?  The function above can be called from anywhere, given the 
two parameters described in the contract, and will get the job done without any 
further knowledge.about the nature of the hash or file involved.  This is, of course, 
a highly generic example.  Even for function which are highly specialized to one 
purpose, there are advantages in debugging to such encapsulisation.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to