[Brisbane-pm] Passing references into Arrays
Martin Jacobs
martin_jacobs at optusnet.com.au
Fri Feb 23 20:20:26 PST 2007
Hi folks,
Having read through the relevant parts of 'Programming Perl', 'Perl
Best Practices' and PerlRefTut, I am still getting to grips with
referencing and dereferencing, and I'm trying to formulate some
conventions to make my code readable (or at least internally
consistent). With this in mind, I've been trying to establish a 'one
size fits all' way to pass variables into subs. I know that this
might be inefficient for subs with only a couple of variables, but
some of my subs need a dozen or so variables. Furthermore, I need to
pass references. I'm going to be dealing with some arrays that are 7
million lines long, so I don't want to copy the arrays if I can help
it. The data structures of the arrays are simple, its just that they
are big.
In following 'Perl Best Practices', I have set up the attached two
versions of a 'conventional' program, which I intend to use as a kind
of template for my 'real' program (its called PERRMOSS). They
actually do what they are supposed to do, though I don't have the
experience to fully explain why. I would be grateful for some sanity
checking here.
If you've been reading my earlier posts, you will note that I'm not
trying to use prototypes in these programs.
A couple of questions;
Perl Best Practices says 'Use a hash of named arguments for any
subroutine that has more than three parameters'. OK, that's PERRMOSS.
I have varied the advice, though, in that I have set up a hash of
references to the variables (because some of the arrays will be very
big). This does seem rather convoluted because you've got to set up a
hash with all the keys, then pass the hash to the sub, then look in
all the keys in the sub to find what you're looking for. Does this
approach make more defensive programming, or less? If the keys are to
make sense, then they should have long names, like the names of the
values, but then there's the danger of mixing up the key with the value.
Version 1 works well, but the syntax is cluttered because each
operation has to derefence each variable at every step.
Version 2 attempts to get round this by dereferencing everything at
the start of the sub. Does this mean it copies the variables in the
sub? Oddly, and I don't know why, if you hash out line 31, the sub
applies the operation to the referent outside the sub, @k. I thought
that the referent would not change until you get to the end of the
sub, which is what happens for $i, @j or %l. I take this as
signifying that the code is not robust, so it is possibly not a good
strategy to pursue.
Thanks in advance for any help.
(Yes, I know the testprint line numbers in Version 2 are not accurate)
Regards,
Martin
Visit my website...
http://web.mac.com/martin_jacobs1
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/brisbane-pm/attachments/20070224/f49bcb52/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Perrmoss_Conventions_version01.pl
Type: text/x-perl-script
Size: 3014 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/brisbane-pm/attachments/20070224/f49bcb52/attachment.bin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/brisbane-pm/attachments/20070224/f49bcb52/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Perrmoss_Conventions_version02.pl
Type: text/x-perl-script
Size: 3224 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/brisbane-pm/attachments/20070224/f49bcb52/attachment-0001.bin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/brisbane-pm/attachments/20070224/f49bcb52/attachment-0002.html
More information about the Brisbane-pm
mailing list