Functions can be declared inside of other functions, so I thought
doing the following was pretty slick:


main () {

  [...]

  case "${var}" in
  ( 'case 1' )
    func1 () {
      [...]
    }
    func2 () {
      [...]
    }
  ;;
  ( 'case 2' )
    func1 () {
      [...]
    }
    func2 () {
      [...]
    }
  ;;
  [...]
  esac

  [...]

}

[...]

main "${@}"


I just now experimented with placing
local func1 func2
and
local -f func1 func2
before the case switch, and that made no difference to whether those
functions would show up in the output from
declare -pf
placed immediately after the call to main () at the end of the script.

The documentation doesn't imply this should work. I'm not going to
claim this is a bug.

A use case where you might actually require functions not defined at
global scope would be pretty niche. Something with recursion might,
for instance.

And there is a reasonable workaround: just define all the variations
on each function outside of the body of any other function, obviously
with different names. Then set a variable to the name of the function
you need in any given situation and expand that variable before any
arguments the function might take where you need to call it.

Still, if it can be declared within the body of a function, maybe
there should be a way to make its definition local to that function's
scope.

Zack

Reply via email to