where is upvar

Kragen Sitaker kragen at dnaco.net
Fri Sep 22 04:09:47 CEST 2000


In article <39CAACC4.1F7E8EE0 at san.rr.com>, Darren New  <dnew at san.rr.com> wrote:
>Kragen Sitaker wrote:
>> It is natural, but it could also be considered a bad idea.  If it's a
>> set of variables specified on the command line, it's definitely a bad
>> idea; if it's a set of variables specified in the script, it is a good
>> idea.  
>
>No, the point is to not pass the variables at all. For example, in the code
>I was writing, I was parsing RFC822 style messages. There was a call
>   getHeaders $message
>that would set a bunch of variables like
>   msg-Subject
>   msg-From
>   msg-To
>and so on. It would only set the variable if the header was there,

So the set of variables is specified in the input, not in the script.

> and it would put "msg-" on the front, so it was easy to avoid
> conflicts, and do things like

That keeps it from being nearly as bad an idea as what I was thinking
of :)

>  if {[info exists msg-Subject]} {
>    set msg-Subject "Re: $msg-Subject"
>  } else {
>    set msg-Subject "No subject"
>  }
>
>Now, Python has more sophisticated types than Tcl natively does, so
>returning a message object with getattr and setaddr overridden perhaps would
>work better.

You wouldn't even need to get to that level of complexity; just create
a msg object and set msg.Subject, msg.From, msg.To, and so forth.

If you were setting Subject, From, and To, you might have a valid
argument that the notation was nicer, but IMHO, the advantages of that
arrangement are outweighed by the accidental-conflict problems.

>I could have returned a hash as well, but the local variables
>in Tcl are like a hash anyway, just as they are in Python.  But it's not at
>all unnatural to set variables in the caller from Tcl.

It's not unnatural in Perl, either, but I have to admit it doesn't seem
like a good idea to me either place.  Packaging the msg stuff into a
single object, instead of making it be half a dozen, means that you can
do things like handling multiple msgs in the same function, passing
messages around from function to function, iterating over the
properties of the message, and so forth; and dumping your local
variables in the debugger becomes more convenient.

Hashes in Tcl are really ugly, unfortunately; Perl is better, but you
still have to say things like $msg{'Subject'}, with five noise
characters drowning out the ten content characters, and it gets even
worse if you return the hash by reference: $msg->{'Subject'}.  (You can
leave out the '' under a set of circumstances I don't thoroughly
understand.) Python/JavaScript object attributes or REBOL pathnames
reduce the overhead to a single inoffensive '.' or '/' character, which
seems much saner..
-- 
<kragen at pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
Perilous to all of us are the devices of an art deeper than we ourselves
possess.
                -- Gandalf the Grey [J.R.R. Tolkien, "Lord of the Rings"]



More information about the Python-list mailing list