Scope rules for reading localvariables after LOCALVARS=0?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Dick99999
Pro Scripter
Posts: 84
Joined: Thu Nov 27, 2008 10:25 am
Location: Netherlands

Scope rules for reading localvariables after LOCALVARS=0?

Post by Dick99999 » Fri Aug 13, 2010 2:23 pm

The LOCALVARS description mentions the creation of variables and their local scope if LOCALVARS=1, even for existing global vars.

What about reading varables after LOCALVARS=0, but before the subroutine will end? Is access to the local variables guaranteed?
i.e

Code: Select all

SRT>sub
   let>LOCALVARS=1
       let>K=1
   let>LOCALVARS=0
      // is result always equal to the value of  the local K of the subroutine?
      let>result=k
end>sub

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Post by gdyvig » Fri Aug 13, 2010 3:03 pm

Hi Dick,

In beta testing local variables were always available prior to leaving the SRT. Global variables were always available both inside and outside the SRT.

Code: Select all

SRT>sub
   let>LOCALVARS=1
       let>K=1
   let>LOCALVARS=0
      // is result always equal to the value of  the local K of the subroutine?
      let>result=k
   MDL>sub(%depth%): K:%K%
   MDL>sub(%depth%): result:%result%
end>sub

gosub>sub
MDL>main: K:%K%
MDL>main: result:%result%
Another area you may want to examine is nested gosubs. It is possible to have several instances of the same SRT with local variables at different levels.

Code: Select all

SRT>sub
   let>depth=%depth%+1
   let>LOCALVARS=1
   if>depth<3
       let>K=%depth%
   endif
   let>LOCALVARS=0
      // is result always equal to the value of  the local K of the subroutine?
      let>result=k
   MDL>sub: K:%K%
   MDL>sub: result:%result%
   wait>1
   if>depth<4
       gosub>sub
   endif
end>sub

Let>depth=0
gosub>sub
MDL>main: K:%K%
MDL>main: result:%result%
Try running the above in Trace mode.
Note that highest level of a variable is available within a SRT.
This is similar to local variables as used in Perl (but not like "MY" variables)

When using localvars avoid goto's.

Gale

Dick99999
Pro Scripter
Posts: 84
Joined: Thu Nov 27, 2008 10:25 am
Location: Netherlands

assignments after localvar is zero

Post by Dick99999 » Mon Aug 16, 2010 2:17 pm

My original question should read "Scope rules of variables after LOCALVARS=0? in stead of focusing on reading and on local variables only. There is nothing in the documentation about that general issue.

Here is another result, where an assignment to a local variable of the previous level (the caller of the subroutine) takes place. Nice that it works that way, but documented now?

Code: Select all

// initiate push/pop level
  let>nLV=0

**BREAKPOINT**
  gosub>parsePorts,portsAll
   // display portsAll is OK and no portIn or PortOut defined
  let>endResult=portsAll_1
**BREAKPOINT**


      SRT>parsePorts
         // parameters name of array i.e. portsAll
         gosub>pushLV,1
         let>nameResult=parsePorts_VAR_1
         let>portsIn_1=99
         let>portsOut_1=88888
         gosub>arraySortNum,portsIn,portsOut
         let>LOCALVARS=0
         let>%nameResult%_1=portsOut_1
         gosub>popLV
      END>parsePorts

            SRT>arraySortNum
                 // parameters name of inarray and of out array
               gosub>pushLV,1
               let>nameIn=arraySortNum_VAR_1
               let>nameOut=arraySortNum_VAR_2
               // do some work on arraySortNu+1m_VAR_1
               let>result=%nameIn%_1+1
               let>LOCALVARS=0
               // return result in array, not global but to local array of preveous level
               let>%nameOut%_1=result
               // restore LOCALVARS
               gosub>popLV
            END>arraySortNum

   // push and pop saved state of localvars
   SRT>pushLV
      let>saveLV_0=LOCALVARS
      let>LOCALVARS=0
      let>nLV=nLV+1
      let>saveLV_%nLV%=saveLV_0
      let>LOCALVARS=pushLV_VAR_1
   END>pushLV

   SRT>popLV
      let>LOCALVARS=0
       let>saveLV_0=saveLV_%nLV%
      let>nLV=nLV-1
      let>LOCALVARS=saveLV_0
   END>popLV

**BREAKPOINT**


Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts