Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:05:30+00:00 2026-06-06T15:05:30+00:00

I have a question about upvar command in TCL. Using upvar command, we have

  • 0

I have a question about upvar command in TCL. Using upvar command, we have have a reference to a global variable or a local variable in other procedure. I saw the following code:

proc tamp {name1 name2} {
    upvar $name1 Ronalod
    upvar $name2 Dom 
    set $Dom "Dom"
}

this procedure is called as tamp name1 name2 , and there is no global variables name1, name2 defined outside of it, how this upvar works in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:05:32+00:00Added an answer on June 6, 2026 at 3:05 pm

    When you call upvar 1 $foo bar, it finds the variable in the caller’s scope whose name is in the foo variable, and makes the local bar variable into an alias for it. If the variable doesn’t exist, it is created in an unset state (i.e., the variable record exists but it has no value. In fact, the implementation uses a NULL to represent that information, which is why Tcl has no NULL equivalent; NULL indicates non-existence) but the link is still created. (It only gets torn down when the local scope is destroyed or if upvar is used to point the local variable at something else.)

    So let’s look at what your code is really doing:

    proc tamp {name1 name2} {
        upvar $name1 Ronalod
        upvar $name2 Dom 
        set $Dom "Dom"
    }
    

    The first line says that we’re creating a command called tamp as a procedure, that that procedure will have two mandatory formal arguments, and that those arguments are called name1 and name2.

    The second line says that we’re binding a variable name in the caller (the 1 level indicator from my earlier explanation is optional, but strongly advised in idiomatic code) that is given by the name1 variable (i.e., the first argument to the procedure) to the local variable Ronalod. Henceforth, all accesses to that local variable (until the end of the stack frame’s life) will be actually performed on the bound variable in the caller.

    The third line is pretty much the same, except with name2 (second argument) and Dom (local variable).

    The fourth line is actually pretty funky. It reads the Dom variable to get a variable name (i.e., the variable named in the second argument to the procedure call) and sets that named variable to the value Dom. Remember, in Tcl you use $ to read from a variable, not to talk about the variable.

    The result of the procedure call will be the result of the last command in its body (i.e., the literal Dom since set yields the contents of the variable as its result, a value it has just assigned). (The last line is wholly uninteresting as it is just the end of the procedure body.)

    The net result of calling this command is actually going to be pretty much nothing at all unless the second argument names a variable containing either Ronalod or Dom. That’s pretty confusing. Of course, the confusing bit is really that funky set with a variable first argument. (That’s almost always a bad idea; it’s an indicator of Bad Code Smell.) Had you used this instead, things would have been simpler:

    set Dom "Dom"
    

    In this case, the variable that Dom is coupled to (i.e., the one named by the second argument to the procedure) would be set to Dom; the variables would in effect be being passed-by-reference. That extra $ makes a huge difference!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a question about stored procedure: How to retrieve the variable's value from stored
I have question about global variable in Objective-C. I have two .m files :
I have a question about arithmetic behaviour in R. Regard the following piece of
I saw a lot of examples of CopyStream implementation but I have question about
I have a question about stopping spoofed form submissions. How about if by using
I have question about throw. How will the throw work in the following code?
I have question regarding about finding subview using '.tags' in one UIView. for (UIView
I have a question about using streams in .NET to load files from disk.
I have a question about local classes in Java (classes that declares in the
I have a question about the images displaying using a function getImage_w($image,$dst_w), which takes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.