Re: add a way to declare global variables

2009-12-14 Thread Marc Herbert
Bernd Eggink a écrit :
 To avoid misunderstandings, let me add that you are right (only) with 
 respect to variables being used _without_ declaration.

OK, but not having to explicitly declare variables is a feature that
most people expect from dynamic languages, so you can hardly blame them
for doing this all the time in shell.

IMHO Python gets this global/local (implicit) declaration story
right. Of course it is easier when you do not have 30 years of POSIX
legacy to support (but just the experience).

http://docs.python.org/tutorial/controlflow.html#defining-functions





Re: add a way to declare global variables

2009-12-14 Thread Bernd Eggink

Am 12/14/09 13:37, schrieb Marc Herbert:

Bernd Eggink a écrit :

To avoid misunderstandings, let me add that you are right (only) with
respect to variables being used _without_ declaration.


OK, but not having to explicitly declare variables is a feature that
most people expect from dynamic languages, so you can hardly blame them
for doing this all the time in shell.


I don't blame anybody. I'm talking about the fact that associative 
arrays are an exception from what most people expect from dynamic 
languages, as they _have_ to be explicitly declared. This doesn't 
bother me, but I would prefer to have a choice between local and global 
declaration anyway.


--
Bernd Eggink
http://sudrala.de




Re: add a way to declare global variables

2009-12-12 Thread Bernd Eggink

Am 12.12.2009 02:11, schrieb Matthew Woehlke:

konsolebox wrote:

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 thought variables in functions were /always/ global unless declared
local?


It's the other way round. Regarding typeset and declare, the man page 
says:  When used in a function, makes  each name local, as with the 
local command.  So within a function, typeset, declare, and local are 
synonyms. Using 'local' outside a function is an error, so IMHO this 
command is completely redundant. It _would_ make some sense, however, if 
its counterpart 'global' existed, as it could help clarify the intended 
usage of the variable.


Bernd

--
Bernd Eggink
http://sudrala.de




Re: add a way to declare global variables

2009-12-12 Thread Bernd Eggink

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.


Bernd

--
Bernd Eggink
http://sudrala.de




Re: add a way to declare global variables

2009-12-12 Thread Chris F.A. Johnson
On Sat, 12 Dec 2009, Bernd Eggink wrote:

 Am 12.12.2009 02:11, schrieb Matthew Woehlke:
  konsolebox wrote:
   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 thought variables in functions were /always/ global unless declared
  local?
 
 It's the other way round. Regarding typeset and declare, the man page says: 
 When used in a function, makes  each name local, as with the local command. 
 So within a function, typeset, declare, and local are synonyms. Using 'local'
 outside a function is an error, so IMHO this command is completely redundant.
 It _would_ make some sense, however, if its counterpart 'global' existed, as
 it could help clarify the intended usage of the variable.

   Of the three, local is the only command I use. It says exactly what
   it does.


-- 
   Chris F.A. Johnson, webmaster http://woodbine-gerrard.com
   ===
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)




Re: add a way to declare global variables

2009-12-12 Thread Stephane CHAZELAS
2009-12-12, 11:28(+01), Bernd Eggink:
 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.
[...]

For information, zsh has the -g typeset option for that.

-- 
Stéphane


Re: add a way to declare global variables

2009-12-12 Thread Bernd Eggink

Am 12.12.2009 11:24, schrieb Bernd Eggink:

Am 12.12.2009 02:11, schrieb Matthew Woehlke:

konsolebox wrote:

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 thought variables in functions were /always/ global unless declared
local?


It's the other way round. Regarding typeset and declare, the man page
says:  When used in a function, makes each name local, as with the
local command.  So within a function, typeset, declare, and local are
synonyms.


To avoid misunderstandings, let me add that you are right (only) with 
respect to variables being used _without_ declaration. The problem is 
that associative arrays are the only kind of variables that _must_ be 
declared, since something like a[foo]=bar without a preceding 
declaration would create an indexed array (and set a[0] to bar).


Bernd

--
Bernd Eggink
http://sudrala.de




Re: add a way to declare global variables

2009-12-12 Thread DennisW
On Dec 12, 4:24 am, Bernd Eggink mono...@sudrala.de wrote:
 Am 12.12.2009 02:11, schrieb Matthew Woehlke:

  konsolebox wrote:
  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 thought variables in functions were /always/ global unless declared
  local?

 It's the other way round. Regarding typeset and declare, the man page
 says:  When used in a function, makes  each name local, as with the
 local command.  So within a function, typeset, declare, and local are
 synonyms. Using 'local' outside a function is an error, so IMHO this
 command is completely redundant. It _would_ make some sense, however, if
 its counterpart 'global' existed, as it could help clarify the intended
 usage of the variable.

 Bernd

 --
 Bernd Egginkhttp://sudrala.de

$ vars () { var1=123; local var2=456; }
$ vars
$ echo var1=$var1 var2=$var2
var1=123 var2=

How is that the other way around?


Re: add a way to declare global variables

2009-12-12 Thread Stephane CHAZELAS
2009-12-12, 12:37(-05), Chet Ramey:
 On 12/12/09 5:24 AM, Bernd Eggink wrote:

 It's the other way round. Regarding typeset and declare, the man page
 says:  When used in a function, makes  each name local, as with the
 local command.  So within a function, typeset, declare, and local are
 synonyms. Using 'local' outside a function is an error, so IMHO this
 command is completely redundant. It _would_ make some sense, however, if
 its counterpart 'global' existed, as it could help clarify the intended
 usage of the variable.

 It might be considered redundant, but just consider it a more mnemonic
 synonym for declare/typeset.  `local' appeared in an earlier version of
 the Posix standard, though `typeset' never did.
[...]

local is also mandated by the LSB spec for sh as well (Linux
standard base). So you can reasonably expect to be able to use
it (though only in local varname) on any script meant for a
recent Linux sh.

-- 
Stéphane


Re: add a way to declare global variables

2009-12-12 Thread 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?

Chet


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/




Re: add a way to declare global variables

2009-12-12 Thread Bernd Eggink

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.


Regards,
Bernd

--
Bernd Eggink
http://sudrala.de




Re: add a way to declare global variables

2009-12-11 Thread Matthew Woehlke

konsolebox wrote:

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 thought variables in functions were /always/ global unless declared local?

--
Matthew
Please do not quote my e-mail address unobfuscated in message bodies.
--
[Microsoft's] license tells you what a netbook is. -- Steve Ballmer





add a way to declare global variables

2009-12-10 Thread konsolebox
Good day,

The new feature, associative arrays in bash 4.0 was very useful but I
can't find any way to declare them as global when declaring inside a
function.  By default all variables are set local when using declare
and typeset and there are no other ways to declare an associative
array but through the said functions.

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.

Cheers,
konsolebox