Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function call as default for parameter

23 views
Skip to first unread message

Cecil Westerhof

unread,
Dec 16, 2017, 9:28:06 PM12/16/17
to
At the moment I have the following function:
proc getHourInDay {{currentTime -1}} {
if {${currentTime} == -1} {
set currentTime [clock seconds]
}
scan [clock format ${currentTime} -format %H] %d
}

But I would like to do something like:
proc getHourInDay {{currentTime [clock seconds]}} {
scan [clock format ${currentTime} -format %H] %d
}

But this gives:
too many fields in argument specifier "currentTime [clock seconds]"

Do I have to do it like the first code, or is there a better way?

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Rich

unread,
Dec 17, 2017, 12:55:34 AM12/17/17
to
Cecil Westerhof <Ce...@decebal.nl> wrote:
> At the moment I have the following function:
> proc getHourInDay {{currentTime -1}} {
> if {${currentTime} == -1} {
> set currentTime [clock seconds]
> }
> scan [clock format ${currentTime} -format %H] %d
> }
>
> But I would like to do something like:
> proc getHourInDay {{currentTime [clock seconds]}} {
> scan [clock format ${currentTime} -format %H] %d
> }
> ...
>
> Do I have to do it like the first code, or is there a better way?

I don't know if it is _better_, but there is _a way_ to create
something that works like you appear to expect the second version to
work.

proc getHourInDay {{currentTime {[clock seconds]}}} {
scan [clock format [subst $currentTime] -format %H] %d
}

But, note, this works only because the expected input to the proc is
usually an integer. For proc's that take strings that might contain
command substitutions ([]'s) it likely won't work as expected, and
potentially could open up something like an sql-injection attack if the
input string were externally user controllable. I.e, think about what
would happen if a user could get a string of the form "[exec format
c:]" (or something similarly dangerous) to be passed to the proc.

Cecil Westerhof

unread,
Dec 17, 2017, 1:59:04 AM12/17/17
to
Yes, that is dangerous. So for the moment I leave it as it is.

Donald Arseneau

unread,
Dec 17, 2017, 5:53:20 AM12/17/17
to
Cecil Westerhof <Ce...@decebal.nl> writes:

> At the moment I have the following function:
> proc getHourInDay {{currentTime -1}} {
> if {${currentTime} == -1} {
> set currentTime [clock seconds]
> }
> scan [clock format ${currentTime} -format %H] %d
> }
>
> But I would like to do something like:
> proc getHourInDay {{currentTime [clock seconds]}} {
> scan [clock format ${currentTime} -format %H] %d
> }
>
> But this gives:
> too many fields in argument specifier "currentTime [clock seconds]"
>
> Do I have to do it like the first code, or is there a better way?

proc getHourInDay {{currentTime now}} {

--
Donald Arseneau as...@triumf.ca

Donald Arseneau

unread,
Dec 17, 2017, 5:56:47 AM12/17/17
to
Donald Arseneau <as...@triumf.ca> writes:

> Cecil Westerhof <Ce...@decebal.nl> writes:
>>
>> But I would like to do something like:
>> proc getHourInDay {{currentTime [clock seconds]}} {
>> scan [clock format ${currentTime} -format %H] %d
>> }
>>
>> But this gives:
>> too many fields in argument specifier "currentTime [clock seconds]"
>>
>> Do I have to do it like the first code, or is there a better way?
>
> proc getHourInDay {{currentTime now}} {


Sorry, please ignore. "now" only works for clock scan, not
clock format. Pity.

--
Donald Arseneau as...@triumf.ca

Bezoar

unread,
Dec 17, 2017, 10:51:48 AM12/17/17
to
this works for me use eval and escape the []'s :

proc getHourInDay {{currentTime "\[ clock seconds\]" } } {
return [scan [eval clock format ${currentTime} -format %H ] %d ]
}

puts "[getHourInDay]"
puts "[getHourInDay 2323232323 ]"

I get :
9
1
as output
0 new messages