{"id":783,"date":"2020-11-12T02:10:59","date_gmt":"2020-11-12T10:10:59","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=783"},"modified":"2020-11-12T02:10:59","modified_gmt":"2020-11-12T10:10:59","slug":"135-global-namespace-in-tcl","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2020\/11\/135-global-namespace-in-tcl\/","title":{"rendered":"#135 Global namespace in tcl"},"content":{"rendered":"<p>#135 Global namespace in tcl<\/p>\n<p>It is possible to reference global variables inside procedures using global namespace indicator:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nset ::element_list &quot;1 2 3 4 5&quot;;\r\nproc ::element_info {} {\r\n   puts $::element_list;\r\n   set ::node_list &quot;10 20 30 40&quot;;\r\n   puts $::node_list;\r\n}\r\nelement_info;\r\n1 2 3 4 5\r\n10 20 30 40\r\n\r\nputs $::element_list;\r\n1 2 3 4 5\r\n\r\nputs $::node_list;\r\n10 20 30 40\r\n<\/pre>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#Script begins.\r\n\r\n#Define a variable in the global scope.\r\nset f &quot;This is a global variable&quot;;\r\n\r\n#This global procedure will generate an error because the\r\n#global variable f is not in the scope.\r\nproc testproc1 {} {\r\n   catch {puts $f} g;\r\n   puts $g;\r\n}\r\n\r\n#This procedure will not generate an error because the\r\n#global variable f is localized with the global command.\r\nproc testproc2 {} {\r\n   global f;\r\n   catch {puts $f} g;\r\n   puts $g;\r\n}\r\n\r\n#A call to delete the namespace clears variables within\r\n#that namespace.\r\ncatch {namespace delete ::testspace};\r\n\r\n#The namespace eval command creates a new namespace.\r\nnamespace eval ::testspace {\r\n   variable fg &quot;This is a namespace variable&quot;;\r\n   \r\n   #Export a single procedure for use later.\r\n   namespace export tproc3\r\n}\r\n\r\n#Procedures can be organized into namespaces.\r\nproc ::testspace::tproc1 {} {\r\n   #Global procedures do not require a namespace indicator.\r\n   testproc1;\r\n   testproc2;\r\n\r\n   #Procedures within the same scope do not require a \r\n   #namespace indicator.\r\n   tproc2;\r\n   tproc3;\r\n\r\n   #Procedures from other namespaces must be explicitly declared\r\n   #unless a namespace import is used.\r\n   catch {oproc1} ds;\r\n   puts $ds;\r\n   ::otherspace::oproc1;\r\n}\r\n\r\n#This namespace procedure will generate an error because the\r\n#namespace variable is not in the scope.\r\nproc ::testspace::tproc2 {} {\r\n   #The catch command is used to prevent the script from\r\n   #exiting with an error.\r\n   catch {puts $fg} g;\r\n   puts $g;\r\n}\r\n\r\n#This namespace procedure will not generate an error because the\r\n#namespace variable is localized.\r\nproc ::testspace::tproc3 {} {\r\n   #The variable command, if referring to an existing\r\n   #namespace variable and not making an assignment,\r\n   #allows local access to the variable.\r\n   variable fg;\r\n   catch {puts $fg} g;\r\n   puts $g;\r\n}\r\n\r\n#A call to delete the namespace clears variables within\r\n#that namespace.\r\ncatch {namespace delete ::otherspace};\r\n\r\n#Creating a second namespace.\r\nnamespace eval ::otherspace {\r\n   variable fh &quot;This is a different namespace variable&quot;;\r\n}\r\n\r\nproc ::otherspace::oproc1 {} {\r\n   #The namespace import command can be used to\r\n   #localize procedures.\r\n   namespace import ::testspace::tproc3;\r\n   variable fh;\r\n   catch {puts $fh} h;\r\n   puts $h;\r\n   tproc3;\r\n\r\n   #namespace origin returns original namespace of the proc.\r\n   puts &#x5B;namespace origin tproc3];\r\n}\r\n\r\n#Run the primary procedure\r\n::testspace::tproc1\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ncan't read &quot;f&quot;: no such variable\r\nThis is a global variable\r\n\r\ncan't read &quot;fg&quot;: no such variable\r\nThis is a namespace variable\r\n\r\ninvalid command name &quot;oproc1&quot;\r\nThis is a different namespace variable\r\n\r\nThis is a namespace variable\r\n\r\n::testspace::tproc3\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#135 Global namespace in tcl It is possible to reference global variables inside procedures using global namespace indicator: set ::element_list &quot;1 2 3 4 5&quot;; proc ::element_info {} { puts $::element_list; set ::node_list &quot;10 20 30 40&quot;; puts $::node_list; } element_info; 1 2 3 4 5 10 20 30 40 puts $::element_list; 1 2 3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[24,27],"tags":[],"class_list":["post-783","post","type-post","status-publish","format-standard","hentry","category-hypermesh","category-tcl"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-cD","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":781,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/11\/134-global-variable-in-tcl\/","url_meta":{"origin":783,"position":0},"title":"#134 Global variable in tcl","author":"gantovnik","date":"2020-11-12","format":false,"excerpt":"#134 Global variable in tcl [code language=\"python\"] set f \"This is a global variable\"; #This procedure cannot access the global variable f. proc testproc1 { } { catch {puts $f} val; puts $val; } #This procedure can access the global variable f using the global command. proc testproc2 { }\u2026","rel":"","context":"In &quot;HyperMesh&quot;","block_context":{"text":"HyperMesh","link":"https:\/\/gantovnik.com\/bio-tips\/category\/hypermesh\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1339,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/01\/133-create-vectors-and-planes-using-tcl-in-hypermesh-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":783,"position":1},"title":"#250 Global variable using :: prefix in tcl","author":"gantovnik","date":"2022-01-05","format":false,"excerpt":"Qualifying a global variable is done by prefixing the variable name with the :: character sequence. This is a special case of namespace qualification where the :: prefix indicates that the variable resides in the global namespace. Example: [code language=\"python\"] set globalvar 100.2 proc test1 {} { puts \"test1: $::globalvar\"\u2026","rel":"","context":"In &quot;tcl&quot;","block_context":{"text":"tcl","link":"https:\/\/gantovnik.com\/bio-tips\/category\/tcl\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1341,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/01\/133-create-vectors-and-planes-using-tcl-in-hypermesh-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":783,"position":2},"title":"#251 Global variable using global keyword in tcl","author":"gantovnik","date":"2022-01-05","format":false,"excerpt":"The global variable can be declared with the global keyword. Example: [code language=\"python\"] set globalvar 100.2 proc test1 {} { global globalvar puts \"test1: $globalvar\" } proc test2 {} { global globalvar puts \"test2: $globalvar\" } test1 test2 [\/code] Result: [code language=\"python\"] test1: 100.2 test2: 100.2 [\/code]","rel":"","context":"In &quot;tcl&quot;","block_context":{"text":"tcl","link":"https:\/\/gantovnik.com\/bio-tips\/category\/tcl\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1337,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/01\/133-create-vectors-and-planes-using-tcl-in-hypermesh-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":783,"position":3},"title":"#249 Creating namespace in tcl","author":"gantovnik","date":"2022-01-05","format":false,"excerpt":"Namespace is a container for a set of identifiers that are used to group variables and procedures. Example: [code language=\"python\"] namespace eval MyMath { # Create a variable inside the namespace variable myResult } # Create procedures inside the namespace proc MyMath::Add {a b } { set ::MyMath::myResult [expr $a\u2026","rel":"","context":"In &quot;tcl&quot;","block_context":{"text":"tcl","link":"https:\/\/gantovnik.com\/bio-tips\/category\/tcl\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1331,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/01\/133-create-vectors-and-planes-using-tcl-in-hypermesh-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":783,"position":4},"title":"#246 Create a procedure with variable number of arguments in tcl","author":"gantovnik","date":"2022-01-04","format":false,"excerpt":"An example of a procedure with variable number of arguments is [code language=\"python\"] proc avg {numbers} { set sum 0 foreach number $numbers { set sum [expr $sum + $number] } set average [expr $sum\/[llength $numbers]] return $average } puts [avg {50 50 50 60}] puts [avg {50 50 50}]\u2026","rel":"","context":"In &quot;tcl&quot;","block_context":{"text":"tcl","link":"https:\/\/gantovnik.com\/bio-tips\/category\/tcl\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1333,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/01\/133-create-vectors-and-planes-using-tcl-in-hypermesh-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":783,"position":5},"title":"#247 Create a procedure with default arguments in tcl","author":"gantovnik","date":"2022-01-04","format":false,"excerpt":"An example of a procedure with procedure with default arguments is [code language=\"python\"] proc add {a {b 2000} } { return [expr $a+$b] } puts [add 22 10] puts [add 22] [\/code] Result: [code language=\"python\"] 32 2022 [\/code]","rel":"","context":"In &quot;tcl&quot;","block_context":{"text":"tcl","link":"https:\/\/gantovnik.com\/bio-tips\/category\/tcl\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=783"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/783\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}