bug-bash
[Top][All Lists]
Advanced

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

Re: YAQAGV (Yet Another Question About Global Variables)


From: Pierre Gaston
Subject: Re: YAQAGV (Yet Another Question About Global Variables)
Date: Tue, 23 Aug 2011 16:58:22 +0300

On Tue, Aug 23, 2011 at 4:42 PM, Steven W. Orr <steveo@syslang.net> wrote:
> I made a decision to implement the require module written by Noah Friedman
> that comes in the examples part of the bash distro. This is the trick for
> implementing the provide / require functionality of features.
>
> 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.

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"



reply via email to

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