>>>>> "OC" == Owen Chavez <owen.chavez314...@gmail.com> writes:

  OC> I've started experimenting with the use of subroutines in place of
  OC> large, repeating blocks of code in a few of my programs.  Without
  OC> exception, these subroutines require variables defined outside of
  OC> the subroutine.  Unless I'm mistaken, the only way to make these
  OC> variables available within the subroutine is to define them as
  OC> global variables using "our."

experimenting? as in maybe they would help your code? drop the
experiment and just use subs. there is no way it won't improve your code
massively. even for non-repeating code, subs organize your code so you
can see what is happening. long linear programs are the worst form of
coding style other than spaghetti code.

as for your scoping issues, the way to use subs best is to pass
arguments to them. needing globals means your program is poorly
designed. you can group related variables into a hash and pass a
reference to that. there are many ways but globals are rarely actually
needed.


  OC> As I replace code (and, I'm sure, as I move forward with new
  OC> tasks), this is going to involve a lot of finding the original
  OC> variable declaration to change "my" to "our."  Is there some way
  OC> around this?  What are the disadvantages to using "our" in a
  OC> fairly widespread manner, to avoid having to waste time going back
  OC> and making the substitutions.

our declares a lexical short name for a GLOBAL variable in the symbol
table. it means that variable can be access and modified from anywhere
in the program. nasty. my variables are scoped to the smallest enclosing
block or file (or eval string but ignore that). this keeps them safe
from other code which could do harm.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to