Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Creating A Clean Environment For Autoloaded Functions
- X-seq: zsh-users 17511
- From: Russell Harmon <russ@xxxxxxxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Subject: Re: Creating A Clean Environment For Autoloaded Functions
- Date: Sun, 30 Dec 2012 16:02:02 -0500
- Cc: zsh-users@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=ZPToQW6iY4mbPOq4VqKua71HNX4ISPLoRHxJu6RhIyE=; b=ps1MWc3LdeEmfbOsR48UXJVewXyAluYqU4EED+YWws7FaZ3ZtYkDNnyRLIUDH1wj3e kMQ4Ko8A0wmYpKinE4S4ywgJMpE3IAHsl8T5QI5M5Su1ydjhfSZEmFrujvfNnnRtsdKm Jjdcr5IE1Roo61kU34CapZ39fs+twO4M7Gg2Y3VOWUqjnVwN7Upk3xGpri10WwZR6Wq3 DkKOPbyoxw/tK5yQ8k7/OfMRsPBuXKrux1QODCF6NsoDr5WSyHDirg3aj5UDLunyA+0D VEoeg1HhoDi4YOiE8JPykrCI0PgsNuNQN7qgYTClp8e6EDQGm8LGJI2M+IOw9/Xqszc2 cusQ==
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eatnumber1.com; s=google; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=ZPToQW6iY4mbPOq4VqKua71HNX4ISPLoRHxJu6RhIyE=; b=hZoMdniopmsql+eD2V2xf9NvOx6Je7x8NZhu2tNtnpwbXYEORdlk3E0dF5KBwscMdX T467CAd6uEf6CYXKAb/ZLlJZK5M8YociHuLXzUByJpe+/E6EbkyQ3EDURgflUlcz6Xde oTiwTCRkou2nYPMWh/T345S+c/LAzmzAkaOIk=
- In-reply-to: <121230112044.ZM879@torch.brasslantern.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CA+zrezQX3hAh1dcPvi6-W9pJ+goWrm2vzQ6gSP=EAjyuw2micQ@mail.gmail.com> <121230112044.ZM879@torch.brasslantern.com>
- Sender: eatnumber1@xxxxxxxxx
On Sun, Dec 30, 2012 at 2:20 PM, Bart Schaefer
<schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Dec 30, 4:44am, Russell Harmon wrote:
>>
>> If I want to write a function which can be autoloaded, how can I
>> prevent functions defined outside of my function from being accessible
>> from within my function? For example, I have a function ls { gls "$@"
>> }, and I know of no way to prevent that function from leaking into the
>> definition of all the functions I've autoloaded which use ls.
>
> This amounts to asking how to violate a basic tenet of shell function
> behavior. They're supposed to act like commands that are always at the
> front of the search path, and the search path doesn't change based on
> the order in which every other command was added to it.
>
> The way you avoid this is to write every function that explicitly does
> not want this behavior so as to explicitly override it; for examply by
> using "command ls" instead of "ls".
>
> However, you could do something like this, assuming zsh/parameter is
> loaded:
>
> ls() {
> if (( $#funcstack > 1 ))
> then command ls "$@"
> else gls "$@"
> fi
> }
>
> This will prevent gls from being run inside any other function, although
> it won't bypass the ls wrapper function entirely.
I guess that breaking with this particular shell idiom is what I want
to do. Otherwise, how is one supposed to be able to create
autoloadable functions with reliable and reproducible behavior? I'm
thinking to add an option to autoload to undefine all functions much
like how -U works with aliases.
Basically, I'm trying to follow what the best practices are when
writing autoloadable functions so that they will be able to run
reliably in the largest number of different environments possible.
>
>> Additionally, is it possible to zmodload a module which is
>> automatically unloaded from the environment after my function
>> completes _only if_ that module was not already loaded?
>
> There isn't a property of modules that supports this, but you can do it
> yourself explicitly with something like this:
>
> {
> zmodload -e zsh/mathfunc # for example
> local unload_mathfunc=$?
> zmodload zsh/mathfunc
>
> : do what you need to with math functions
>
> } always {
> (( unload_mathfunc )) && zmodload -u zsh/mathfunc
> }
>
> If the zsh/parameter module is not itself one of the ones you care about
> unloading in this way, you can be more generic:
>
> {
> zmodload zsh/parameter
> local -a existing_modules
> existing_modules=( ${(k)modules[(R)loaded]} )
>
> : do whatever zmodloads you want ...
>
> } always {
> # Requires zsh 5.0, otherwise you need a loop
> zmodload -u ${(k)modules[(R)loaded]:|existing_modules}
> }
>
> I'm not sure that zmodload is clever enough to notice that inter-module
> dependencies have been satisfied by the list provided to -u and thus
> unload the modules in the correct order to be sure it succeeds, so a
> bit of tweaking to the above may be required for full generality.
Hmm, I think I'm realizing that loading & unloading of dependent
modules every time a function is called might not be exactly what I
want. I think what I'm looking for is more like zsh modules where you
have hook functions for when the function is loaded & unloaded so that
you can unload relevant modules when no longer needed. That has it's
own difficulties however because it'd be hard to tell if some other
function loaded after you were loaded needs a module which you are
about to unload.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author