zsh-users
 help / color / mirror / code / Atom feed
* Static variables?
@ 1998-06-14  4:55 Johan Sundström
  1998-06-14  6:38 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Johan Sundström @ 1998-06-14  4:55 UTC (permalink / raw)
  To: Zsh-list

Is it possible to use c-style "static" variables in my functions, or do I
have to stick to global ones? My precmd function doesn't get too happy when
its variables are tampered with, and it's quite slow enough as it is without
sanitytests. Having some variables accessable only from a certain function
(precmd in this case) would help a lot and unclutter the huge list of global
variables a bit, while at it.

If it's possible, how is it done?

/Johan Sundström


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Static variables?
  1998-06-14  4:55 Static variables? Johan Sundström
@ 1998-06-14  6:38 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 1998-06-14  6:38 UTC (permalink / raw)
  To: Johan Sundström, Zsh-list

On Jun 14,  6:55am, =?iso-8859-1?Q?Johan_Sundstr=F6m?= wrote:
} Subject: Static variables?
}
} Is it possible to use c-style "static" variables in my functions, or do I
} have to stick to global ones?

The only "persistent" variable namespace is the global one.

That said, there may be a way to get the effect you want, because the
"local" namespace has dynamic scope.  Here's how:

First, rename your precmd as precmd_body.

Next, create a function precmd_setup which assigns values to the variables
you wish to make static.

Now create a new precmd which does the following:
- Declares "local" all the variables that are assigned in precmd_setup
- Executes precmd_setup (this assigns to the locals of precmd)
- Executes precmd_body (this references the locals of precmd)
- Redefines precmd_setup to assign the new values of the locals to
  the appropriate names, e.g. eval function precmd_setup \{ ... \}

Finally, be sure that none of the affected variables is declared local
in precmd_body (so it will inherit the locals from precmd).

The tricky bit is "redefines precmd_setup."  Depending on the values of
the variables in question, it may be difficult to generate on the fly a
new definition that performs the correct assignments.  Just how ugly are
the contents of the variables you're trying to preserve?

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1998-06-14  6:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-06-14  4:55 Static variables? Johan Sundström
1998-06-14  6:38 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).