[Top][All Lists]

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

Re: add a way to declare global variables

From: Bernd Eggink
Subject: Re: add a way to declare global variables
Date: Sat, 12 Dec 2009 22:58:50 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; de; rv: Gecko/20090915 Thunderbird/3.0b4

Am 12.12.2009 21:41, schrieb Chet Ramey:
On 12/12/09 5:28 AM, Bernd Eggink wrote:
Am 10.12.2009 10:40, schrieb konsolebox:

I hope the development team will also consider adding a way in bash to
declare global variables inside a function perhaps either with an
option in typeset or declare like -g (same as zsh) and/or a builtin
function like global as similar to local.

I second that. I'm missing this feature badly. Presently, you can only
get around this restriction by using a lot of ugly and dangerous 'eval's.

OK.  I'm interested in use cases for this feature.  What are you trying
to do that you're missing it "badly?"

One example is introducing some kind of data encapsulation in bash, or even a modest form of object orientation. You may laugh at it, but I think it might be useful in complex scripts. I experimented a little and came up with 'classes' consisting of a function, and 'objects' consisting of a function (representing the interface) plus an equally named associative array (containing the data). The difficulties arise if you want to create an object 'a' of class X by saying

   X create a

You can declare a function 'a' dynamically within function X (using eval), but you can't declare a (global) associative array 'a' there. So that's where a global declaration would be desirable. The alternative is to print the declarations and eval the whole thing in the current environment, but that is very inconvenient and can cause nasty quoting problems.


Bernd Eggink

reply via email to

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