Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

YAQAGV (Yet Another Question About Global Variables)

1 view
Skip to first unread message

Steven W. Orr

unread,
Aug 23, 2011, 9:42:21 AM8/23/11
to bug-...@gnu.org
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.

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

Greg Wooledge

unread,
Aug 23, 2011, 9:52:54 AM8/23/11
to Steven W. Orr, bug-...@gnu.org
On Tue, Aug 23, 2011 at 09:42:21AM -0400, Steven W. Orr wrote:
> 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.

bash 4.2 adds "declare -g" to create global variables from within a function.

Pierre Gaston

unread,
Aug 23, 2011, 9:58:22 AM8/23/11
to Steven W. Orr, bug-...@gnu.org
On Tue, Aug 23, 2011 at 4:42 PM, Steven W. Orr <ste...@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"

Steven W. Orr

unread,
Aug 23, 2011, 10:06:55 AM8/23/11
to bug-...@gnu.org
On 8/23/2011 9:52 AM, Greg Wooledge wrote:
> On Tue, Aug 23, 2011 at 09:42:21AM -0400, Steven W. Orr wrote:
>> 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.
>
> bash 4.2 adds "declare -g" to create global variables from within a function.

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

0 new messages