validProc

Difference between version 2 and 3 - Previous - Next
'''Name:''' ''validProc''

'''Description:'''
Determines if a procedure is valid in the context
within which it is called.  Just allows a clean test
for usability.

'''Parameters:'''
''name'' - a proc name or wildcard in any number of forms:
  1. foo - a plain proc name in ANY child namespace
  2. ::foo* - all procs in the namespace foo
  3. ::foo::* - same
  4. ::foo::valid* - all procs named like this one

'''Usage:'''
       if { ! [ validProc "procname" ] } { complain } 

'''Comments:'''
Best made use of in the negated sense as shown above.

======
proc validProc { { name * } } {
     set namesp [ namespace children ]
    
     ;## if the argument was namespace aware
     if { [ regexp {::} $name ] } {
         set namesp {}
     }
    
     ;## if the arg was of the form ::foo*
     if { [ regexp {^(::[^:]+)\*} $name -> tmp ] } {
         set name ${tmp}::*
     }
    
     ;## examine all namespaces visible locally
     foreach ns $namesp {
        if { ! [ regexp {::$} $ns ] } {
            ns ${ns}::
        }
        if { [ llength [ info commands $ns$name ] ] } {
            return 1
        }
     }
    
     ;## if the argument was a non-namespace proc
     if { [ llength [ info commands $name ] ] } {
         return 1
     }
     return 0
}
======

--[Phil Ehrens]
<<categories>> Dev. Tools>>