bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

language inconsistency(wart) & RFE


From: Linda Walsh
Subject: language inconsistency(wart) & RFE
Date: Fri, 16 Oct 2015 18:18:21 -0700
User-agent: Thunderbird

Ok, thinking this from a different way.

shopt -s implicit_vars_local
or
shopt -s localize_func_implicit_vars.... whatever...

Right now, in a function, you *can* use local in a function
to make a local var.  Thing is, both 'declare' and 'typeset' also
make a *local* var in a function, unless the "-g" switch is used.

I.e. All standard, overt ways (local declare typeset) of creating
a var in a function all result in it being local, BUT, (and I think this is an ugly wart), any *implicit vars* without local, or the misleading declare or typeset, become global.

examples:  In these two for statements, used in functions, 'i'
becomes global:

 for((i=0; i<10; ++i)); do : done
 for i in {1..10}; do : done

And same with 'myarray':
 readarray myarray=$(echo "one";echo "two")

and reads and assignments, and 'lets'
 read ln < <(echo "one"; echo "two")
 ln2="one two"
 read ln3 <<< "one two"

but if this isn't a potential for confusion:

function aa {
read ln < <(echo "one"; echo "two")
ln2="12"
read ln3 <<< "one two"
ar1=(one two) typeset -i ln2=34
typeset -a ar1=(three four)
}
whence aa
aa is a function
aa () { read ln < <(echo "one"; echo "two");
   ln2="12";
   read ln3 <<< "one two";
   ar1=(one two);
   typeset -i ln2=34;
   typeset -a ar1=(three four)
}
aa
declare -p ln ln2 ln3 ar1
declare -- ln="one"
declare -- ln2="12"
declare -- ln3="one two"
declare -a ar1='([0]="one" [1]="two")'

!!! -- sure looks like I was trying to set the "type" of ln2 and ar1... boy could that be confusing...
....
So, how about a "shopt" to declare that **what's implicity declared in funcs, stays in funcs**
maybe shopt -s vegasvars ?.....

but seriously -- it's so odd that anything you declare explicitly becomes local, while implicit vars default to global --
I know standards and compat must keep it that way... BUT
it would have made more sense to have
implicit vars in a function always be 'local'
and maybe have explicit declarators be global
(which has effectively been done with -g)...but
originally, I also thought it strange that 'declare/typeset'
were equivalent to 'local' inside a function.

This way, you wouldn't have to change any syntax
parsing functions and there certain isn't ANYTHING
that would look like perl, even though perl was
originally designed to be like shell with many shell standard
functions built-in.

???




reply via email to

[Prev in Thread] Current Thread [Next in Thread]