TCL/Expect using puts to add data to a file

Hi all,

I'm trying to do something that might be basic, but it is not working for me, and I suspect I'm missing something.
I appreciate if you can shed a light or offer an alternative.

In expect script, I'm opening a file i.e:

set file [open "myfile.txt" w]

I have a simple proc:

proc a {} { puts "Hello world"}

And I'm trying to do the follow:

puts $file [a]

And:

close $file

I wanted to see "Hello world" in myfile.txt, but it doesn't work, myfile.txt remains empty.

How can I make it work?
what is wrong the script?

Thanks in advanced

P.S I tried google it a lot, but couldn't find anything so far

Modify your procedure:

% tclsh
% set file [open "myfile.txt" w]
file10505448
% proc a {} { return "Hello world" }
% puts $file [a]
% close $file
% exec cat "myfile.txt"
Hello world

works like charm
Thanks lot

---------- Post updated at 09:00 PM ---------- Previous update was at 08:19 PM ----------

I'm trying also, to use expect commands in that procedure:

proc commands {} {
        expect "#"
        send "show run\n"
        expect "#"
        send "show ver"
        expect "#"
        return
}

and send it to a file

puts $file [commands]

But it returns empty.
How can I make it works?

Thanks in advanced

Sorry for the delay, but I'm quite busy these days.
What exactly do you want store in the file, could you post a sample of the desired output file?