Re: Unsetting local variables

2017-03-17 Thread Martijn Dekker
Op 17-03-17 om 23:08 schreef Thorsten Glaser:
> Martijn Dekker dixit:
> 
>> Op 17-03-17 om 20:53 schreef Thorsten Glaser:
>>> Even mksh’s “global” builtin does not access the global scope.
>>> It’s simply a “typeset” that’s not also “local”.
>>
>> But why the separate builtin (which is unique to mksh, if I'm not
>> mistaken) rather than 'typeset -g' as in zsh, bash 4 and yash?
> 
> Oh. I was not aware of that.
> 
> I think that’s sufficient to deprecate “global” even. Thanks!

Neat. :)

One nitpick with the new man page phrasing though:

| Create parameters in global scope, not in local scope.

That's not quite accurate, as was also explained by the old explanation
for the 'global' command which was patched out: typeset -g "does not
allow a function called from another function to access a parameter at
truly global scope, but only prevents putting an accessed one into local
scope." IOW, it may still inherit the enveloping local scope from a
calling function.

- M.



Re: Unsetting local variables

2017-03-17 Thread Thorsten Glaser
Martijn Dekker dixit:

>Op 17-03-17 om 20:53 schreef Thorsten Glaser:
>> Even mksh’s “global” builtin does not access the global scope.
>> It’s simply a “typeset” that’s not also “local”.
>
>But why the separate builtin (which is unique to mksh, if I'm not
>mistaken) rather than 'typeset -g' as in zsh, bash 4 and yash?

Oh. I was not aware of that.

I think that’s sufficient to deprecate “global” even. Thanks!

bye,
//mirabilos
-- 
>> Why don't you use JavaScript? I also don't like enabling JavaScript in
> Because I use lynx as browser.
+1
-- Octavio Alvarez, me and ⡍⠁⠗⠊⠕ (Mario Lang) on debian-devel


Re: Unsetting local variables

2017-03-17 Thread Martijn Dekker
Op 17-03-17 om 20:53 schreef Thorsten Glaser:
> Even mksh’s “global” builtin does not access the global scope.
> It’s simply a “typeset” that’s not also “local”.

But why the separate builtin (which is unique to mksh, if I'm not
mistaken) rather than 'typeset -g' as in zsh, bash 4 and yash?

Please consider adding 'typeset -g' as a synonym for 'global' for those
of us who target multiple shells:

Index: funcs.c
===
RCS file: /cvs/src/bin/mksh/funcs.c,v
retrieving revision 1.327
diff -u -r1.327 funcs.c
--- funcs.c 12 Mar 2017 02:31:26 -  1.327
+++ funcs.c 17 Mar 2017 21:42:39 -
@@ -784,7 +784,7 @@
}

/* see comment below regarding possible opions */
-   opts = istset ? "L#R#UZ#afi#lnprtux" : "p";
+   opts = istset ? "L#R#UZ#agfi#lnprtux" : "p";

builtin_opt.flags |= GF_PLUSOPT;
/*
@@ -829,6 +829,9 @@
case 'f':
func = true;
break;
+   case 'g':
+   localv = false;
+   break;
case 'i':
flag = INTEGER;
basestr = builtin_opt.optarg;
Index: mksh.1
===
RCS file: /cvs/src/bin/mksh/mksh.1,v
retrieving revision 1.427
diff -u -r1.427 mksh.1
--- mksh.1  12 Mar 2017 02:35:19 -  1.427
+++ mksh.1  17 Mar 2017 21:42:41 -
@@ -4803,7 +4803,7 @@
 .Pp
 .It Xo
 .Ic global
-.Oo Op Ic +\-alpnrtUux
+.Oo Op Ic +\-aglnprtUux
 .Op Fl L Ns Op Ar n
 .Op Fl R Ns Op Ar n
 .Op Fl Z Ns Op Ar n
@@ -4815,7 +4815,7 @@
 .Xc
 .It Xo
 .Ic typeset
-.Oo Op Ic +\-alpnrtUux
+.Oo Op Ic +\-aglnprtUux
 .Op Fl LRZ Ns Op Ar n
 .Op Fl i Ns Op Ar n
 .No \*(Ba Fl f Op Fl tux Oc
@@ -4880,6 +4880,11 @@
 .It Fl f
 Function mode.
 Display or set functions and their attributes, instead of parameters.
+.It Fl g
+Disable the local scope for parameters.
+Equivalent to using the
+.Ic global
+command.
 .It Fl i Ns Op Ar n
 Integer attribute.
 .Ar n



Re: Unsetting local variables

2017-03-17 Thread Dan Douglas
On 03/17/2017 12:07 PM, Stephane Chazelas wrote:
> Dan is describing what I can only explain as a bug or at least a
> very surprising feature:

The way Chet has described it in the past it sounded like an intentional
compromise to make outer scopes accessible without the also possibly
surprising interaction with outer scopes (which may be unpredictable
thanks to dynamic scope). But yes it isn't what you'd guess. It should
be documented at least.


Re: Unsetting local variables

2017-03-16 Thread Dan Douglas
On 03/14/2017 05:08 AM, Jean Delvare wrote:
> So internally bash does make a difference between a variable being
> null and a variable being unset. A bash variable can actually have 4
> distinct states: non-existent, existent but unset, set but null, and
> non-null. "typeset", "local" and "unset" all put the variable into
> state "existent but unset" (unlike mksh.)

It depends on context and the type of variable involved. Arrays complicate
things and namerefs _really_ complicate things. In bash, declaring a
simple local scalar, assigning it a value, then unsettling it within
the same scope brings the variable back to the same "hidden local"
state it is in when declared local but not given a value. However,
unsetting the local from a child scope does fully destroy the local,
uncovering the variables in parent scopes.

Thus bash's is actually the most flexible of the dynamic scope systems
in some ways. If you want bash to behave like mksh just define a wrapper
function for unset (I usually name it `unset2'). Contrast with zsh,
which has no way to unset a local (last I checked, which was years
ago). In addition, bash has `declare -g' for directly acting on the
global scope. It has a few good uses e.g. for setting special variables
like PATH or LC_*.