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

procedure arguments in one list

3 views
Skip to first unread message

Christian Bednarek

unread,
Nov 27, 1998, 3:00:00 AM11/27/98
to
Hi!

Maybe I am a bit dump or I misread smth. (maybe because of my really poor
english ;).

following situation:

I have a set of proc's, which get their arguments in one list (this are
pairs of -arg value, e.g. foobar {-arg value -arg1 value1}.

procedure foobar passes this along to another procedure, called splitArgs,
which returns a list like { {-arg value} {-arg value} } and does
error-handling. For this, splitArgs itself passes the list to another proc
etc...

Now my problem/misunderstanding (?): with each instance, this args-list is
passed along, it gets one more nesting with {}. What was {-a b} at first,
becomes to { { { -a b} } } after two passings.
This is very annoying, since I never know, how many passings there were.
I could do a [join $args] in every proc, but that seems a bit "dirty".

What's my mistake?

btw.: I use 80p2

thanks in advance (and sorry if this is a faq which I missed),

Christian


Andreas Kupries

unread,
Nov 29, 1998, 3:00:00 AM11/29/98
to

Christian Bednarek <cbed...@abo.rhein-zeitung.de> writes:

> Hi!
>
> Maybe I am a bit dump or I misread smth. (maybe because of my really poor
> english ;).
>
> following situation:
>
> I have a set of proc's, which get their arguments in one list (this are
> pairs of -arg value, e.g. foobar {-arg value -arg1 value1}.

[explanation elided]

As you gave no example script showing the described behaviour I have
to guess about the cause of the problem. To me it looks as if your
procedures are defined like

proc p1 {args} {
...
p2 $args
...
}
proc p2 {args} {
...
pX $args
...
}

etc.

If that is not the case skip the rest of this posting [*].

Ok, with that behind us the explanation of the problem:

'args' is a special argument name (see 'proc' manpage), it causes the
system to collect all additional arguments into a list, thus allowing
us to write vararg-procedures. If it is the sole argument, tcl will
wrap all arguments into a list.

This behaviour does *not* change with the number or type of
arguments. Calling p1 with a single argument will wrap that argument
into a list. If the argument was already a list you get a list with a
single element, which is a (multi-element) list. And to make this
structure clear another level level of braces is added by the
interpreter.

So, if you repeatedly and recursively call procedures as defined above
you get a list wrapped in a list, wrapped in a list, ... Ok it should
be clear now.

Solution:
You don't have to change the procedure definition (its
argument list), but you have to call them differently:

proc p1 {args} {
...
eval p2 $args
~~~~
...
}

The eval strips one level of nesting from args, thus calling p2 not
with a single list-argument, but the contents of the list as multiple
arguments. The 'args' argument of 'p2' then causes the system to
reaggregate them into a list of keys and values.

[*] And repost with some code.

--
Sincerely,
Andreas Kupries <a.ku...@westend.com>
<http://www.westend.com/~kupries/>
-------------------------------------------------------------------------------

0 new messages