I love it. It works great. I just ran into one little snag. Not a show
stopper, but I'm wondering if there's a better way to deal with it.
Some of the modules I have are designated as library modules, and so are used
as args to require. Since require is a function, any variables that are
declared in a required module which declare global variables using typeset
then become local variables to the require function. Then after the modules
are loaded, the variables that used to be global are gone.
I went through the library modules and removed the typeset commands from all
of the global variables and that seems to fix it. What got lost however was
the functional part of the typset commands. For example, a variable was
declared as
typeset -a foo=( abc def ghi )
and now it has to be changed to
foo=( abc def ghi )
No big loss. But I also had some things declared as constants or integers (or
both).
typeset -i x1=44
typeset -ir x2=55
I'm not 100% sure that that won't break something, but for now I'm just glad
that they didn't go out of scope.
Is there a trick to allow declarations like I used to do in a module and
preserve their characteristics if they are sourced in from inside a function?
If there's an answer, I'll get it here.
TIA
--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
bash 4.2 adds "declare -g" to create global variables from within a function.
Take care that the difference between integer and string can be subtle:
$ typeset -i i=0;while ((i<=10));do i=$i+1;done;echo $i # works
11
$ unset i;i=0;while ((i<=10));do i=$i+1;done;echo $i #works too but....
0+1+1+1+1+1+1+1+1+1+1+1
i is evaluated inside (( )) so 0+1+1+1+1+1+1+1+1+1+1+1 is evaluated at
each iteration and it "works"
Hmm. I'm wondering if a trap might solve my problem. Bash4.2 doesn't help
since we're on 4.0.35, but it would have been nice.
Something like...
# old code
require()
{
...
source $fn # fn is global
}
#new code
trap { source $fn; } SIGUSER1
require()
{
# set the trap. Rats! I need to set it to a function.
fn=something_I_want
kill -USR1 $$
# unset the trap handler