11.07.2015 Views

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Caché</strong> <strong>ObjectScript</strong> CommandsStart ; This routine uses the legacy IF command syntaxSET x=1IF x=1 DO Sub1(x) ; sets $TEST to TRUE (1)ELSE DO Sub2(x)QUITSub1(y) ; a subroutine that evaluates a FALSE IFWRITE !,"hello from subroutine 1"IF y=2 WRITE " - IF in Sub1 was TRUE" ; Set $TEST to FALSE (0)ELSE WRITE " - IF in Sub1 was FALSE"QUITSub2(z) ; another subroutineWRITE !,"hello from subroutine 2"QUITAt first glance, you might expect that Start will call only Sub1 and then exit.In fact, execution of this code produces the following:USER>DO ^Starthello from subroutine 1 - IF in Sub1 was FALSEhello from subroutine 2This unexpected behavior results from the fact the $TEST value is reset in Sub1, that causes<strong>Caché</strong> to execute the ELSE command in Start. The processing sequence is as follows:1. <strong>Caché</strong> evaluates the IF command expression in Start as TRUE. It sets $TEST to TRUEand calls Sub1.2. <strong>Caché</strong> evaluates the IF command expression in Sub1 as FALSE. It sets $TEST to FALSEand then executes the following ELSE command.3. <strong>Caché</strong> returns to Start when it encounters the QUIT.4. <strong>Caché</strong> executes the ELSE in Start and performs the DO call to Sub2. It executes theELSE because $TEST was set to FALSE in Sub1, replacing the TRUE value set by theIF command in Start.To produce the expected behavior, you could replace the ELSE in either Start or Sub1with an additional IF. For example, you might recast Start as follows:StartSET x=1IF x=1 DO Sub1(x)IF x'=1 DO Sub2(x)QUITSee Also• GOTO command• XECUTE command• FOR command32 <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!