Programming/TclTk

Tcl namespace eval command

Hunveloper 2022. 9. 16. 14:48
728x90

namespace eval command

  1. namespace가 존재하지 않으면 생성
  2. 해당 namespace내에서 제공된 코드를 실행

 

namespace안에서 정의된 프로시저들 (default namespace가 :: 이기 때문에 모든 프로시저들임)

  • 만약 절대 namespace앞에 있는 경우 지정한 namespace안에서 명령을 정의
  • 만약 상대 namespace앞에 있는 경우 현재 namespace를 기준으로 해당 명령을 정의
namespace eval bob {
    namespace eval joe {
        proc proc1 {} {}
    }
    proc proc2 {} {}
    proc ::proc3 {} {}
    proc joe::proc4 {} {}
}
proc proc5 {} {}
proc bob::joe::proc6 {} {}
proc ::bob::joe::proc7 {} {}

이 명령들은 아래의 명령과 동일함

::bob::joe::proc1
::bob::proc2
::proc3
::bob::joe::proc4
::proc5
::bob::joe::proc6
::bob::joe::proc7

 

 

 

 

더보기

The namespace eval command

 

  1. Creates the namespace if it doesn't exist
  2. Runs the code supplied to it from within that namespace

Procs defined from within a namespace (which is all procs, since the default namespace is ::)

  • If preceded by an absolute namespace, will define that command in the specified namespace
  • If preceded by a relative namespace, will define that command relative to the current namespace

As such

namespace eval bob {
    namespace eval joe {
        proc proc1 {} {}
    }
    proc proc2 {} {}
    proc ::proc3 {} {}
    proc joe::proc4 {} {}
}
proc proc5 {} {}
proc bob::joe::proc6 {} {}
proc ::bob::joe::proc7 {} {}

The following commands will exist

::bob::joe::proc1
::bob::proc2
::proc3
::bob::joe::proc4
::proc5
::bob::joe::proc6
::bob::joe::proc7

Notice that commands in the global namespace, when called from the global namespace, can be preceeded by a :: or not. The same is true of commands in any namespace.

namespace eval bob {
    proc2 ;# calls ::bob::proc2
    ::proc5 ;# calls ::proc5 (proc5 in the global namespace)
    joe::proc4 ;# calls ::bob::joe::proc4
}

It is worth noting that a raw command name (with no namespace qualifiers at all) will look in the current namespace and then, if it doesn't find the command there, the global namespace.

 

source : how to define a proc in tcl namespace - Stack Overflow

 

 

728x90
728x90