zsh-users
 help / color / mirror / code / Atom feed
From: Russell Harmon <russ@eatnumber1.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: zsh-users@zsh.org
Subject: Re: Creating A Clean Environment For Autoloaded Functions
Date: Sun, 30 Dec 2012 16:02:02 -0500	[thread overview]
Message-ID: <CA+zrezRUYzT0pCYSaFt1tHfPzqiTgpJbaJ2kYZ2z4Ow75XwQAw@mail.gmail.com> (raw)
In-Reply-To: <121230112044.ZM879@torch.brasslantern.com>

On Sun, Dec 30, 2012 at 2:20 PM, Bart Schaefer
<schaefer@brasslantern.com> 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.


  reply	other threads:[~2012-12-30 21:29 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-30  9:44 Russell Harmon
2012-12-30 19:20 ` Bart Schaefer
2012-12-30 21:02   ` Russell Harmon [this message]
2012-12-30 22:12     ` Bart Schaefer
2012-12-31 23:30   ` Han Pingtian
2013-01-02  5:15     ` PATCH and more remarks on parameter expansion docs Bart Schaefer
2013-01-02  8:32       ` Han Pingtian
2013-01-02 16:46         ` Bart Schaefer
2013-01-02 23:28           ` Han Pingtian
2013-01-03 19:42             ` Bart Schaefer
2013-01-03 22:18               ` Han Pingtian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+zrezRUYzT0pCYSaFt1tHfPzqiTgpJbaJ2kYZ2z4Ow75XwQAw@mail.gmail.com \
    --to=russ@eatnumber1.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).