From: Dean Chao on
Hi guys,

How to use global variable as the default value for procedure parameter. I.e.,


proc foo {{bar $var}} {
puts $bar
}

set var "hello world!"
foo




When I do this, stdout prints $var instead of "hello world!"



thx
Dean
From: Jeff Godfrey on
Dean Chao wrote:
> Hi guys,
>
> How to use global variable as the default value for procedure parameter.
> I.e.,
>
>
> proc foo {{bar $var}} {
> puts $bar
> }
>
> set var "hello world!"
> foo
>
>
>
>
> When I do this, stdout prints $var instead of "hello world!"

How about something like...

proc foo {{bar ""}} {
if {$bar eq ""} {
set bar $::var
}
puts $bar
}

Jeff
From: rf on
set var default
# set the default value of bar to the actual value of var
proc foo [list [list bar $var]] {return $bar}

Roland
From: rf on
to reflect changes to var:

proc foo [list [list x var]] {
upvar $x z
puts $z
}


From: Cameron Laird on
In article <h7osv7$sve$1(a)news.eternal-september.org>,
Jeff Godfrey <jeff_godfrey(a)pobox.com> wrote:
>Dean Chao wrote:
>> Hi guys,
>>
>> How to use global variable as the default value for procedure parameter.
>> I.e.,
>>
>>
>> proc foo {{bar $var}} {
>> puts $bar
>> }
>>
>> set var "hello world!"
>> foo
>>
>>
>>
>>
>> When I do this, stdout prints $var instead of "hello world!"
>
>How about something like...
>
>proc foo {{bar ""}} {
> if {$bar eq ""} {
> set bar $::var
> }
> puts $bar
>}
>
>Jeff

OR
proc foo [list [list bar $::var]] {
do_the_real_work_of_foo $bar
}
 |  Next  |  Last
Pages: 1 2 3 4 5
Prev: How to detect if event loop is running ?
Next: regexp