[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: using a variable from global scope or checking whether it is local t
From: |
Christoph Anton Mitterer |
Subject: |
Re: using a variable from global scope or checking whether it is local to some scope |
Date: |
Thu, 19 Oct 2023 23:49:50 +0200 |
User-agent: |
Evolution 3.50.0-1 |
Hey Greg.
On Thu, 2023-10-19 at 12:47 -0400, Greg Wooledge wrote:
> I don't think either of these can be achieved non-destructively.
Yeah, thought so too.
> You could just keep unsetting the variable until it no longer exists,
> but that destroys all the local instances of the variable throughout
> the call stack.
Actually if I save the original value every time, and unset as long as
it exists... I could restore the global one.
But as you said, all local instances would still be gone, and that
would of course also cause major breakage.
> My first thought was to use "declare -g var" inside the innermost
> function, but this doesn't work. Or at least, I couldn't make it
> work.
Well it seems to only set the var in global context, not change the
scope from the current var of that name?
> If you really need this functionality, I don't think bash is suited.
Nah, I just stumbled over it and got curious.
For my current case I've worked around this by simply giving an error
if my function was called from another function (using the size of
FUNCNAME, yes I know, could in principle be manipulated).
What I would have actually needed in some other context a while a go
was away to prevent that breaking-out.
I.e. when I have:
myfunc()
{
local var
arbitraryOtherFunc()
}
that arbitraryOtherFunc couldn't "break out" by unsetting myfunc's
local var, and then access/unset the earlier ones.
I think dash disallows that, but I guess for bash no such feature is
planned?!
Anyway… thanks :-)
Chris.