1. begin
Pushes a dictionary onto the dictionary stack, making it the current dictionary.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
(empty) |
Dictionary stack increased by one |
1.3. Description
begin pushes dict onto the dictionary stack, making it the current dictionary. This dictionary becomes the first one consulted during:
1.5. Examples
5 dict begin
/x 10 def
/y 20 def
x y add % Uses local definitions
end
% x and y no longer in scope
/outer 5 dict def
outer begin
/outerVar 1 def
10 dict begin
/innerVar 2 def
outerVar innerVar add % Can access outer scope
end
end
/config 10 dict def
config begin
/fontSize 12 def
/fontName /Helvetica def
fontName findfont fontSize scalefont setfont
end
1.6. Common Use Cases
1.6.1. Temporary Namespace
/doWork {
10 dict begin
% All defs here go into local dict
/temp1 value1 def
/temp2 value2 def
% Work with temp variables
end
} def
1.7. Common Pitfalls
dict1 begin
dict2 begin
% Missing ends will overflow stack eventually
| Dictionary Must Be Readable - Dictionary must not have no-access attribute. |
| Use gsave/grestore for Safety - When using begin/end with graphics state changes: |
gsave
mydict begin
% Graphics operations
end
grestore
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Dictionary stack has grown too large |
[ |
Dictionary has no-access attribute |
[ |
No operand on stack |
[ |
Operand is not a dictionary |
1.9. Implementation Notes
-
Dictionary stack typically allows 20-250 dictionaries
-
Standard stack contains at minimum: systemdict, userdict
-
Level 2 adds globaldict to standard stack
-
Stack operations affect name lookup immediately
1.10. Dictionary Stack Structure
User dictionary (via begin)
User dictionary (via begin)
...
userdict
globaldict (Level 2)
systemdict
1.11. See Also
-
end- Pop dictionary from stack -
currentdict- Get current dictionary -
def- Define in current dictionary -
dictstack- Examine dictionary stack -
countdictstack- Count stack depth