Rajeshwar
Rajeshwar

Reputation: 11661

storing value into a variable from a returned function in TCL

I have a function that returns a string however I cant store that value into a variable. I tried doing this

% set m [return "This is returned value"]
This is returned value
% puts $m
can't read "m": no such variable

Any suggestions what I might be doing wrong here ? Also I tried something like this

% set m [puts "Test"]
Test
% puts $m
...

the output of m is blank why is that ?

Update :

After looking into this issue. It seems that I have a method that cannot be altered. This method prints an output to the screen. What I want to do is to capture that output in a variable as well.

Upvotes: 3

Views: 5697

Answers (1)

Dinesh
Dinesh

Reputation: 16428

If you want to return value from a function it should something like this

proc value { } {
   return rajesh
}

set result [value]

puts command won't return any value and it will be empty string if you assign it to a variable.

In your first case, you have used return inside the variable assignment like

set m [return "This is returned value"]

Because of return, code will return immediately. It has nowhere to return to. Further code below this of any, won't run at all. That's why the it got failed as can't read "m": no such variable

Upvotes: 5

Related Questions