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

Re: local unfunction



On Fri, Mar 30, 2018 at 9:11 AM, Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
> Is it possible to unfunction something just within another function?  I've
> tried:
>
> unset -fm ....
>
> ... but the functions remain dead after the calling function returns.  I can
> of course just resource them, but I'll bet the unset can be made local.

You can get that effect like this:

% zmodload zsh/parameter
% inner() { print $0 }
% outer() { local +h functions; unfunction inner; which inner }
% which inner
inner () {
    print $0
}
% outer
inner not found
% which inner
inner () {
    print $0
}

HOWEVER, this not "safe" with autoloaded functions that have not been
loaded yet:

% zmodload zsh/parameter
% outer() { local +h functions; unfunction inner; which inner }
% autoload -U -k inner
% which inner
inner () {
    # undefined
    builtin autoload -XUk
}
% outer
inner not found
% which inner
inner () {
    builtin autoload -XU
}

Note that the "# undefined" tag and the -k flag have been lost;
"inner" is no longer a true autoloaded function.  (I think it is
probably a bug that $functions[inner] does not have the -k flag.)



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