[Top] [All Lists]

variables: scoping

2003-04-13 19:31:29

the discussion is getting unwieldy, so I'm trying to break it into
more managable pieces.

Lawrence Greenfield may be right, keeping all variables in one global
scope may cause problems later.  but I don't want to make things very
complicated, either, ie. mandatory declaration of variables before use
seems unnecessary.

therefore, I suggest a modifier :local to SET as a middle ground.  it
will cause the named variable to have block local scope.  if used
outside any blocks, it will have file local scope.  a SET _without_
:local will either change the value of an existing variable if there
is one in scope, or initialise a global variable.

  require [ "variables", "include" ];
  set "foo" "anna"; # a global variable
  set "foo" :local "beatrice"; # this shadows the global variable
  if ... {
      set "foo" "charlize";
      set "foo" :local "deborah"; # shadows the variable with file scope
      # here, ${foo} is "deborah"
  # here, ${foo} is "charlize"
  include "/personal/spamtests";
  # inside spamtests, ${foo} is the global variable with value "anna".
  # spamtests can set new global variables which are visible after
  # the script returns.

this does mean that SET may be used to "declare" variables as well as
"set" their values, but I don't think it is a problem to combine the
two functions into one action.

Kjetil T.

<Prev in Thread] Current Thread [Next in Thread>