11.07.2015 Views

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Caché</strong> <strong>ObjectScript</strong> CommandsIn the following example, assume that the local variable environment of the calling routine(Start) consists of variables a, b, and c. When the DO calls Subr1, the NEW commandredefines Subr1's local variable environment to exclude all variables except c and d.After the NEW, the subroutine's environment consists only of the new variables c and d. Thenew variable c is retained from the calling routine's environment and keeps its existing value.The new variable d is created undefined.The first SET command in Subr1 references c to assign a value to d. The second SET commandassigns a new value (24) to c. When the subroutine QUITs, c will have this updatedvalue (and not the original value of 6) in the calling routine's environment.Start SET a=2,b=4,c=6DO Subr1WRITE !,"c in Start: ",cQUITSubr1 NEW (c,d)SET d=c+cSET c=d*2WRITE !,"c in Subr1: ",cQUITWhen executed, this code produces the following results:c in Subr1: 24c in Start: 24Argumentless NEWThe argumentless NEW provides an empty local variable environment for a called subroutineor user-defined function. The existing local variable environment (in the calling routine) issaved and then restored when the subroutine or function terminates. Any variables createdafter the NEW are deleted when the subroutine or function terminates.If a command follows the NEW on the same line, be sure to separate the NEW commandfrom the command following it by (at least) two spaces.$ETRAP and Special VariablesYou cannot use NEW on special variables; attempting to do so results in a error.The one exception is $ETRAP. When you issue the command NEW $ETRAP, <strong>Caché</strong> createsa new context for error trapping. You can then set $ETRAP in this new context with thedesired error trapping command(s). The $ETRAP value in the previous context is preserved.If you set $ETRAP without first issuing the NEW $ETRAP command, <strong>Caché</strong> sets $ETRAPto this value in all contexts. It is therefore recommended that you always NEW the $ETRAPspecial variable before setting it.98 <strong>Caché</strong> <strong>ObjectScript</strong> <strong>Reference</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!