Mod /
Modlite
Search:  


1.  Modlite Overview

Modlite provides basic modules to reduce complexity while supportting Tcl 8.3+. Modlite provides the core, non-gui functionality of Mod.

Here is a overview of the provided commands:

NameDescription
externPrototype definition for a proc or command
ModThe module command
OptsParse arguments
.[A-Z]*Development/debug utilities: all begin with a dot+Cap
\**Utility procs: all begin with a star
New/Newv/DeleteOO-like namespace-objects

1.1  Extern

The extern command is used for forward declarations and for defining command aliases and auto-loading modules. It is also used by Wize to validate programs.

  extern work {n} {. Int} {A ::sub::work -1}
  extern pet {op args} {} {E source $dir/pet.tcl}
  extern http {op args} {} {E package require http}

1.2  Opts

Opts provides a minimalistic option parser that combines functionality with self documentation:

  proc substr {str args} {
      Opts p $args {
         { -start   0     "The begin" }
         { -last    end   "The end"   }
      }
      return [string range $str $p(-start) $p(-last)]
  }
  substr $a -end 10
  substr $a -start 3 -end 5

1.3  Devel

The devel macros are used for debugging Tcl. Moreover, they can all be mapped to NO-OPS with a single call to [Mod ndebug].

  proc foo {n} {
    .Trace
    .Assert {$n>=0 && $n<=100}
    if {$n == 50} {
       .Warn "foo called with 50"
    }
  }

1.4  Util

Util provides a collection of utilities whose names all start with a star. This ensures that they will not clash with the names of local procedures.

  proc Save {f dat} {
     *catch { file delete $f.bak }
     *fwrite $f $dat
  }

1.5  New/Newv/Delete

New et al are part of Tod, which is used for developing larger, more object oriented applications.

1.6  Mod Sub-commands

Mod provides the following sub-commands:

NameArgumentsDescription
exportargsSetup current namespace as an ensemble (or emulate it)
includefile argsSource .tcl files and/or load shared libraries.
ndebug{level -1}Disable or enable debugging utilities
upcmdsargsImport commands from the parent namespace
upvarsargsUpvar to variables in the parent namespace
usessrcns argsUse commands from other namespaces

There is also a normalize command that works even in Tcl8.3 and in safe interps.

2.  Small Example

Here is a small Mod example:

namespace eval ::dog {
    Mod export
    proc bark {n} { return [incr n] }
    proc feed {n} { return [incr n -1] }
}

dog bark 1

3.  A Multi-namespace Example

In the following example we care for our pets and they thank us!

package require Modlite
#Mod ndebug

namespace eval ::pet {
    Mod export
    variable fed 0
    variable feed 0

    proc echo {str}  {tclLog $str}    
    proc eat {} { variable fed; echo "PET: eat [incr fed]" }
}

namespace eval ::pet::dog {
    Mod export
    Mod uses ..

    proc eat {}  {echo "DOG: eat"}
    proc bark {} {echo "DOG: bark"}
    proc feed {} { variable feed; echo "DOG: feed [incr feed]"; eat}
}

namespace eval ::pet::cat {
    Mod export
    Mod uses ..
    Mod upvars feed fed

    proc meow {} { echo "CAT: meow" }
    proc feed {} { variable feed; echo "CAT: feed [incr feed]"; eat}
}

namespace eval ::mypets {

    Mod uses ::pet ::pet::dog ::pet::cat

    proc main {args} {
        pet cat feed
        pet dog feed
        cat meow
        dog bark
        cat feed
        feed        ;# pet::dog::feed
        bark        ;# pet::dog::bark
        meow        ;# pet::cat::meow
    }

    eval main $argv
}

© 2008 Peter MacDonald

Page last modified on January 31, 2010, at 08:12 AM