Re: "unset var" pops var off variable stack instead of unsetting it

2017-03-22 Thread Chet Ramey
On 3/21/17 3:19 AM, Dan Douglas wrote:

> Also not documented is how a variable declared with declare/typeset is
> distinct from an unset variable.

I don't know, I think this is pretty clear:

"A parameter is set if it has been assigned a value."

The previous paragraph discusses attributes.  The subsequent paragraph
discusses how values are assigned.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: "unset var" pops var off variable stack instead of unsetting it

2017-03-22 Thread Chet Ramey
> The effect of unset on a local was what I had in mind, but really the
> manual says very little about scope. All it says right now is:
> 
> "Variables local to the function may be declared with the local builtin
> command.  Ordinarily, variables and their values are shared between the
> function and its caller."
> 
> Which doesn't exactly describe dynamic scope even for those that know
> what that means.

Here's what I have to start:

   Variables  local to the function may be declared with the local builtin
   command.  Ordinarily, variables and their values are shared between the
   function  and  its  caller.  If a variable is declared local, the vari-
   able's visible scope is restricted to that function  and  its  children
   (including the functions it calls).  Local variables "shadow" variables
   with the same name declared at previous scopes.  For instance, a  local
   variable  declared  in  a  function hides a global variable of the same
   name: references and assignments refer to the local  variable,  leaving
   the  global variable unmodified.  When the function returns, the global
   variable is once again visible.

   The shell uses dynamic  scoping  to  control  a  variable's  visibility
   within  functions.   With  dynamic scoping, visible variables and their
   values are a result of the sequence of function calls that caused  exe-
   cution  to  reach the current function.  The value of a variable that a
   function sees depends on its value within its caller, if  any,  whether
   that  caller  is the "global" scope or another shell function.  This is
   also the value that a local variable  declaration  "shadows",  and  the
   value that is restored when the function returns.

   For  example, if a variable var is declared as local in function func1,
   and func1 calls another function func2, references  to  var  made  from
   within func2 will resolve to the local variable var from func1, shadow-
   ing any global variable named var.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: "unset var" pops var off variable stack instead of unsetting it

2017-03-22 Thread Martijn Dekker
Op 21-03-17 om 16:38 schreef Stephane Chazelas:
> IOW, the work around I was mentioning earlier (of using "local"
> before "unset" to make sure "unset" unsets) doesn't work in that
> case. You'd need to use the same work around as for mksh/yash
> (call unset in a loop until the variable is really unset (with
> the nasty side effect of unsetting the variable in a scope
> you're need meant to tamper with) so you'd want to do it in a
> subshell).

Note that this workaround needs to be applied conditionally on
cross-platform POSIX scripts because you'd get an infinite loop on
recent-ish versions of ksh93 (as of 2010, IIRC). Those ksh93 versions
have BUG_IFSISSET: it is not possible to determine in any normal way if
IFS is set, neither with "${IFS+set}" nor with [[ -v IFS ]]. They always
act as if IFS is set, even when it is unset. This applies to IFS only.

Upon detecting BUG_IFSISSET, modernish applies a workaround to isset()*
that involves analysing field splitting behaviour to distinguish between
empty IFS and unset IFS. So 'isset IFS' is fully cross-platform.

- M.

* https://github.com/modernish/modernish#working-with-variables