Macro Scheduler 15

Scope

By default variable scope is global.  That is, any variable created in your script will be available anywhere else in your script, including in any subroutines.  Any variables created within a subroutine will remain available to the rest of the script.

 

A word or two should be said about other scripts called or included by your script.  The Macro command executes another macro.  That macro is kept separate from the calling macro.  In this instance variables are not shared, so scope could be said to be local.  Using the Macro command is like calling a separate process via the command line.  Variables can be passed into the called macro via command line parameters, but script variables within the code are not shared.  In contrast the Include statement, rather than "call" the included macro file, literally includes the code from that file within the main script. Include is rather like copying and pasting the code from the Include file into the main script file at run time.  Therefore in this instance variables are shared as the Included script becomes part of the main script.

 

Local Scope

 

It is possible to force new variables to have only local scope by setting LOCALVARS to 1.  After setting LOCALVARS to 1 any variable creations or assignations will have local scope.  I.e. if LOCALVARS is 1 and a new variable is created within a subroutine that variable will only have scope within that subroutine.  It will no longer be available to the rest of the script once that subroutine has terminated.

 

With local scope enabled subroutines can still see any "global" variables.  I.e. variables created in the main script or parent subroutine will be available to the subroutine.  But attempting to modify an existing variable while LOCALVARS=1 will create a local version of that variable if it doesn't already exist locally.  In other words all variable assignations or creations will have local scope.

 

Therefore if LOCALVARS has been set to 1 and you need to modify a lower level variable you will need to set LOCALVARS to 0 first.

 

Note that variable look-ups are recursive down through the variable table hierarchy so when retrieving values a match will occur against a parent level variable first. In other words variable look-ups "drill down" through the variable table hierarchy until a match is found.  E.g. if we are at level 2 and there is a variable in the root table (level 0) and another with the same name at the parent level 1, then the value of the parent subroutine's variable will be returned (level 1).  If you want to override this behaviour to specifically force only level 0 look up, set LOCALVARS to 2.

 

Example:

 

Watch what happens when you step through this script line by line.

 

Let>globalA=apple

Let>kk=2

Let>file_1=fred

Let>file_2=sally

Let>file_3=alan

ARC>file,count

ARS>file

 

GoSub>NewSub

 

MessageModal>%file_1% %globalA%

 

SRT>NewSub

    /*

    Set LOCALVARS to 1 to force all variable assignations and creations to have local scope.

    You can still retrieve global variables.  But attempting to modify them while LOCALVARS=1

    will cause creation of a local variable with same name.

    So to modify global variables, set LOCALVARS=0

    Setting LOCALVARS to 0 (default) means variables will be created/modified in the global table

    */

    Let>LOCALVARS=1

 

    Let>kk=0

 

    MessageModal>globalA

 

    GetFileList>%USERDOCUMENTS_DIR%\*.*,filelist,;

    Separate>filelist,;,file

 

    Repeat>kk

      Let>kk=kk+1

      Let>Item=file_%kk%

    Until>kk=file_count

 

    //here we want to set the value of global variable globalA

    Let>LOCALVARS=0

    Let>globalA=pear

 

END>NewSub