Re: [Help-bash] make function local

2015-04-10 Thread Dan Douglas
 On Thu, Apr 9, 2015 at 5:40 PM, Peng Yu pengyu...@gmail.com wrote:
  However, the real problem is that global variable make larger programs
  difficult to maintain. Therefore, the problem becomes severe for
  people using bash for making bash libraries. Any ad hoc use of bash
  can always get away from this problem.

 The solution is namespaces. ksh93 supports namespace blocks (including nested
 namespaces), though it's still quite buggy.

 Note C doesn't have namespaces either and gets by despite being used as
 a library language to a much greater extent than bash. It's worked around with
 naming conventions. There are many prefix conventions even within the
 C standard library and POSIX for various headers - atomic_*, mtx_*, pthread_*,
 sem_*, etc.

 On Fri, Apr 10, 2015 at 8:54 AM, Peng Yu pengyu...@gmail.com wrote:
  You don't know functional programming, do you? That example is
  functional program (not full fledged though) but not OO.
 
  It seems that you don't know its importance, either? With functional
  programming, you basically can make OO on top of it, but not the other
  way around. R has examples on how to make OO using functional
  programing.

 Functional programming is a style, not a language feature. Features that
 facilitate it are common in OO langs.

 C# implements first-class functions with closures by translating them in the IL
 to inner classes that maintain the lexical state. I'm pretty sure Java 8 does
 essentially the same with its lambdas. People have been using the inner class
 trick for ages prior to that.

  function as first class citizen is just one of the feature that makes
  javascript so successful now days.

 The type of function you're talking about isn't merely a local function.
 JavaScript doesn't exactly have local functions, and probably isn't the best
 example because of its numerous ways of defining functions. It also doesn't
 have a separate namespace for functions. No matter how you define them, you're
 likely using sugar that in the end results in a function object that's
 accessed through an identifier that's in the same namespace as any other
 variable (which might be local).

 Bash needs several major features before it could even think about supporting
 function objects.

 First it needs yet another type of function (probably with yet another syntax)
 which supports returning a value, and declaration as an expression so it can
 be passed around and treated as a value.

 At the same time it needs to add some sort of type system so that data other
 than strings (objects or functions) can be bound to variables and formal
 parameters.

 ksh93 has the beginnings of an object system which supports user-defined types,
 but functions are still insufficient for doing anything functional. Some of
 its basic ideas are nice but I have pretty much given up on understanding David
 Korn's design. It's obviously half-baked in its current state and hard to use
 for getting real work done. It's better than nothing I guess.

 Anyway, you can probably do something resembling FP with some combination of
 `typeset -T` wrappers to contain your functions, and `typeset -M` and `-C` to
 move and copy objects around by reference. It's not pretty.

 --
 Dan Douglas



Re: Bash wrongly attaches subcommand stdin on syntax error

2015-04-10 Thread Chet Ramey
On 4/7/15 6:56 AM, Sam Liddicott wrote:

 Bash Version: 4.3
 Patch Level: 11
 Release Status: release
 
 Description:
 Shell wrongly attaches stdin piped to command sequence with syntax error
 
 Repeat-By:
 On a login shell or interactive shell, paste the following command:
 
 for x in 1 ; do echo $( { echo } ) ; done  ( echo touch /tmp/x2 )
 
 The handling of the syntax error will cause stdin of the command
 to become attached to the login shell, which will then execute:
   touch /tmp/x2
 and then logout.

Thanks for the report.  This will be fixed in the next release of bash.

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: [Help-bash] make function local

2015-04-10 Thread Chet Ramey
On 4/10/15 10:13 AM, Eduardo A. Bustamante López wrote:

 - a faster implementation of the variable lookup code

What does this mean, exactly?  Optimizing the existing code paths? (Have at
it.)  Different semantics?  Static as opposed to dynamic scoping?

 - a shopt to disable evaluation of shell code in places like arithmetic
 expansion

This has a chance to get implemented if you can provide enough detail about
how you want it to work.

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: Second trap invocation is ignored?

2015-04-10 Thread Scott Bronson
On Fri, Apr 10, 2015 at 8:16 AM, Chet Ramey chet.ra...@case.edu wrote:
 On 4/6/15 11:58 AM, Greg Wooledge wrote:
  I'd be fine with that, but then why does source ./foo create a DEBUG
  trap at the global scope the *first* time?

 Because there's nothing to save and restore.

Just curious: why not restore the state of emptiness that
existed before?  Why treat these two states differently?
(presence of trap vs. absence of trap)

Other than this minor question, your explanation makes
total sense.  Thanks Chet.

- Scott