On Wed, 18 Jul 2001, Stephanie Stiavetti wrote:

> I'm writing a script to check for validity of form elements.  here's what I
> have so far:
>
> my ($job) = $cgi->param("job");
>
> bla bla bla more script bla bla bla
>
>
> sub fixFailed
> {     my (@failedFields);
>       my ($validForm)="1";
>
>       #validate job selection
>       if (!$job) {
>               $job="job ";
>               push (@failedFields, $job);
>
>               }
>
> my question is, I remember someone a while back mentioning that you should
> NOT redefine global variables in your subroutines... what can I do as an
> alternative?  what do I risk by doing it anyways?

You should pass the global variable into your subroutine as an argument.
Functions that modify global variables that other functions are depending
on can lead to some hard to find bugs.  A function should be like a black
box, data in and data out, decoupled, if possible, from other functions.
Sometimes this rule has to be broken, but it is a basic premise of good
software design.

I came across some code I had to do some bug fixes last week that realyl
had me shuddering.  It was structured like this (mind you, they original
author was not using -w or strict, so the script had all kinds of things
that even Matt Wright would shudder at):

function1 {

  $stuff = "something";
  $junk = "more stuff";

  #code
  #code

  function2();

  #code
  #code
}

function2 {

  $otherjunk = $junk;
  $stuff = $junk + $something;

  #code
  #code
}

-- Brett
                                   http://www.chapelperilous.net/btfwk/
------------------------------------------------------------------------
Zero Mostel: That's it baby!  When you got it, flaunt it!  Flaunt it!
                -- Mel Brooks, "The Producers"


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

Reply via email to