Removes all elements from the operand stack, leaving it empty.
1. Description
The clear operator pops all objects from the operand stack and discards them, regardless of how many elements are present. After execution, the stack is guaranteed to be empty.
This is a drastic operation that should be used carefully, as it discards all stack contents without any possibility of recovery. It’s primarily useful for error recovery, initialization, or resetting to a known state.
This is a Level 1 operator, available in all PostScript implementations.
5. Examples
5.1. Basic Usage
% Clear a stack with elements
1 2 3 4 5 clear
count % Stack: 0
% Clear an already empty stack (safe)
clear
clear % Still empty, no error
5.2. Using in Error Recovery
% Reset stack after error
{
% Some operation that might fail
1 0 div
} stopped {
% Error occurred, clean up
clear
(Error recovered, stack cleared) print
} if
6. Advanced Examples
6.1. Safe Procedure Wrapper
% Execute procedure with stack isolation
/isolatedExec { % proc -> (executes with clean stack)
count array astore % Save current stack
exch exec % Execute with cleared stack
exch aload pop % Restore saved stack
} def
% Usage
1 2 3
{ clear 10 20 add } isolatedExec
% Stack: 1 2 3 (preserved, isolated exec returned nothing)
6.2. Conditional Clear
% Clear only if stack has elements
/clearIfNotEmpty {
count 0 gt {
clear
} if
} def
clear
clearIfNotEmpty % Does nothing (already empty)
1 2 3
clearIfNotEmpty % Clears the stack
7. Edge Cases and Common Pitfalls
clear discards all stack elements without any confirmation or undo capability. Use with caution in production code.
|
7.1. Accidental Data Loss
% BAD: Losing important data
/importantValue 42 def
importantValue
% ... some operations ...
clear % OOPS! Lost the value
% importantValue is gone from stack
% GOOD: Save important values first
/importantValue 42 def
importantValue
/saved exch def % Save before clearing
clear
saved % Restore after clear
7.2. Clear vs cleartomark
% clear removes EVERYTHING
mark 1 2 3 4 5 clear
count % Stack: 0 (mark is gone too!)
% cleartomark removes up to and including mark
mark 1 2 3 4 5 cleartomark
count % Stack: 0 (but stops at mark)
% cleartomark preserves elements below mark
10 20 mark 1 2 3 cleartomark
count % Stack: 10 20 2
7.3. Clear Doesn’t Affect Other Stacks
% clear only affects operand stack
% Dictionary stack and execution stack unchanged
/mydict 10 dict def
mydict begin
/value 42 def
end
1 2 3 clear % Operand stack cleared
mydict /value get % Stack: 42 (dictionary unaffected)
Consider using cleartomark instead of clear when you want to preserve elements below a certain point on the stack.
|
8. Related Commands
-
pop- Remove single element -
cleartomark- Remove elements up to a mark -
count- Check if stack is empty -
mark- Place marker for selective clearing
9. PostScript Level
Available in: PostScript Level 1 and higher
This is a fundamental operator available in all PostScript implementations.
11. Performance Considerations
The clear operator is very fast with O(n) time complexity where n is the number of elements on the stack. However, since it’s typically used with small to moderate stack depths, performance is rarely a concern.
Clearing is faster than individually popping each element.
12. Best Practices
-
Use sparingly: Only clear when you truly need to discard all stack contents
-
Consider alternatives: Use
popfor selective removal orcleartomarkfor bounded clearing -
Error recovery: Excellent for recovering from errors and resetting to known state
-
Document intent: Always comment why you’re clearing the stack
-
Test carefully: Ensure you’re not accidentally discarding needed data
13. See Also
-
Operators Overview - Understanding PostScript operators
-
Stack Operations Guide - Stack manipulation tutorial
-
Error Handling - Using clear for error recovery
-
Stack Manipulation - All stack operators