ObjectScript Hidden Gems by oek76922

VIEWS: 16 PAGES: 15

									      ObjectScript
      Hidden Gems
A random & eclectic selection



         Ian Cargill
  Dendrite Clinical Systems
Dynamic Access to Classes - 1
• Set x=##class(Pkg.MyClass).Reset()
• But what if you don't know class or
  method/property at compile time??
  – $ZOBJMETHOD
  – $ZOBJCLASSMETHOD
  – $ZOBPROPERTY
Dynamic Access to Classes – 2
     Update Array of Properties
 F("Name")=Tom
 F("Age")=10
 Set obj=##class(Pkg.Cls).%New()
 Set fld=$Order(F(fld))
 While fld'="" {
   Set $ZOBJPROPERTY(obj,fld)=F(fld)
   Set fld=$Order(F(fld))
 }
                 Xecute
• Build up a line of code dynamically,
  then execute it!
• Implied function call
• Doesn't run in scope of procedure
• Only access public variables
• For class methods/properties, $ZOBJ better.
• Problems inside functions(){}
               $ZStrip
$ZSTRIP(string,action,remchar,keepchar)
WRITE $ZSTRIP(str,"*A")
WRITE $ZSTRIP(str,"*A'L")
WRITE $ZSTRIP(str,"*A'L","d")
WRITE $ZSTRIP(str,"*WA'L",,"D")
                $Increment
•   Almost trivial, but VERY handy!
•   Atomic Increment of variable
•   Shared counters or arrays
•   Debug! Self indexing array
    – Set ^AA($I^(^AA))="message"
     Structured System Variables
•   $GLOBAL
•   $JOB
•   $LOCK
•   $ROUTINE
         Special Variables
• $QUIT
  – Argument required?? From calling context.
• $ZPARENT / $ZCHILD
  – Allows parent and Child to communicate
  – 0 = Not Jobbed
• $KEY
  – "" if Read timed out!   READ "Name?":5
                  $ZUTIL
• Let it be your friend 
• Grab-bag of options for, well… all sorts of
  things.
• Some in system-wide/process pairs (68|69,
• $ZUTIL(90,10)
  – Tests whether a namespace is defined.
       Sequential File I/O
• OPEN Mode Parameters
  – OPEN "LIST.FILE":"WNS"     Typical
  – D for delete on close!


• CLOSE Mode Parameters            They
 exist!
  – CLOSE file:"D"             Delete
  – CLOSE file:("R":newname)   Rename
^%ZSTART and ^%ZSTOP.
• Catch things happening at a system level
  – SYSTEM: Caché system starts or stops
  – LOGIN: A user performs a login or logout
  – JOB: A JOB begins or ends
  – CALLIN: An external program begins or
    completes a CALLIN
• ^%ZSSUtil for writing to logs
                              ZZDUMP
• Hex Dump Utility
USER>s x="Line 1 with "_$C(9,9)_"Tabs"_$C(13,10)_"Line 2"_$C(13,10)_"Line 3"

USER>w x
Line 1 with            Tabs
Line 2
Line 3

USER>ZZDUMP x
0000: 4C 69 6E 65 20 31 20 77 69 74 68 20 09 09 54 61        Line 1 with ..Ta
0010: 62 73 0D 0A 4C 69 6E 65 20 32 0D 0A 4C 69 6E 65        bs..Line 2..Line
0020: 20 33
                Indirection - 1
• @ Operator
    – Use content of variable, not variable itself.
•   Name indirection
•   Pattern indirection
•   Argument indirection
•   Subscript indirection
•   $TEXT argument indirection
         Indirection - 2
Name Indirection

Set val=5
Set ivar="val"
Set x = @ivar*6     // x=30!
             Indirection - 3
Pattern Indirection

Test(txt)
  If txt?"3A4N" Quit

  Set pat="3A4N"
  If txt?@pat Quit

								
To top