Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [BUG] Trap and local variable



On Oct 11, 11:01pm, Sebastian Gniazdowski wrote:
} Subject: [BUG] Trap and local variable
}
} Hello,
} following code:
} 
} b() { integer locl=1337; globl=2337; a() { echo "$locl, $globl"; };
} trap "a" EXIT }; b
} 
} outputs only 2337.

This isn't a bug.

Function definitions don't nest, so even though you didn't define "a"
until inside of "b", the scope of "a" is a sibling of "b".  Also, the
doc for the "trap" command says:

     If SIG is 0 or EXIT and the trap statement is executed inside the
     body of a function, then the command ARG is executed after the
     function completes.

So $locl is already out of scope when the trap runs, there is no way
for "a" to see it.



Messages sorted by: Reverse Date, Date, Thread, Author