On Wed, May 03, 2006 at 09:17:21AM -0500, Rance Hall wrote:

> Assuming you had a script configuration variable that was used numerous 
> places in your script, further assume that you have "use strict;" in 
> your perl script.
> 
> You could declare the variable "my" and pass that variable to any 
> subroutines that needed it with @_.
> 
> Or you could declare that variable with "local" or some other "scope 
> defining declaration" so that it would be available automatically in any 
> and all subroutines called from the level you declared the var as "local"
> 
> My question concerns a little of form and a little of function
> 
> On the function front:
> 
> which approach is faster?  With one you need a
> 
> my ($arg1, $arg2...) = @_;
> 
> inside each subroutine which should take some time to execute.
> 
> (I apologize if the syntax isn't correct for the above line, I don't 
> have my notes available and I'm questioning the use of the parenthesis, 
> but you should be able to get the idea, which is all I'm after)
> 
> On the form front, Ive noticed that the scripts are not as readable for 
> me, and by extension probably any other people who use my script.
> 
> What are your thoughts on choosing which approach to take?  I'm sure 
> both are technically correct, but I'm sure there are places where one 
> approach should be preferred over the other.

First, forget about speed.  Why do you care whether one approach is
faster than another?  If your script is "fast enough" don't waste time
and effort even thinking about it.  And if it's not fast enough it's
unlikely that tweaking a few variables is going to make it fast enough.

So, we're left with style.  Choose the style which makes your program
most understandable.  If you only have a few parameters then passing
them around may be fine.  If there are lots you might want to put them
into global variables, by which I mean package scoped lexical variables.
I always start global variables with an uppercase letter so they are
immediately recognisable.

An approach I frequently use is to have a global hash %Options in which
I keep all the  ... wait for it ... global options.  I also pass this
hash into GetOptions from Getopt::Long to get the command line options.
I find this approach works well.

Another option, if you are using OO, is to make the options part of the
object in which case they get passed around for free.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to