14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

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.

Chapter 8 Programming Methods 199<br />

Advanced Scoping and Namespaces<br />

Note: Names Default To Here(1) defines a mode for a particular script. It is not a global function.<br />

One script can have this mode turned on, while another script can have it turned off. The default setting is<br />

off.<br />

In JMP 8 and earlier, the only method to the insulate scripts was to use lengthy names that were less likely to<br />

collide with names in other scripts. Using Names Default To Here(1) makes this technique<br />

unnecessary.<br />

Local() creates local scopes only in specific contexts within a script and cannot enclose a longer script with<br />

interacting functions, while Names Default To Here(1) creates a local scope for an entire script.<br />

If you have simple scripting needs, this single command might be sufficient.<br />

Handling Unqualified Named Variable References<br />

The Names Default To Here() function determines how unqualified named variable references are<br />

resolved. Explicitly scoping a variable using here:var_name always works, whether Names Default To<br />

Here() is on or off. See “Scoped Names” on page 201 for details about here and other scopes.<br />

Enabling the Names Default To Here mode associates a scope called Here with an executing script. The Here<br />

scope contains all of the unqualified named variables that are created when they are the target of an<br />

assignment (as an L-value). In JMP 8 and earlier, these variables normally would have been placed in the<br />

Global scope. Using a Here scope keeps variables in multiple executing scripts separate from each other,<br />

avoiding name collisions and simplifying the scripting and management of variable name collisions. You can<br />

still share information using the Global scope.<br />

Names Default To Here and Global Variables<br />

Run this example script one line at a time to see how the Names Default To Here() function changes<br />

the resolution of variable names.<br />

Example Script<br />

a=1;<br />

Names Default To Here(1);<br />

a=5;<br />

show(global:a, a, here:a);<br />

global:a = 1;<br />

a = 5;<br />

here:a = 5;<br />

1. Run the first line to create a global variable named a that holds the value 1.<br />

2. Run the second line to turn on the Names Default To Here mode.<br />

3. Run the third line to create a new variable named a in the local space that holds the value 5. This line<br />

does not change the value assigned to the global variable a.<br />

4. Run the fourth line to see how scoped and unscoped variables are resolved.<br />

The unqualified a is resolved to here:a. If Names Default To Here() were not on, a would be resolved<br />

to the global variable named a.

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

Saved successfully!

Ooh no, something went wrong!