Z Shell“自动加载"内置 - 它有什么用? [英] Z Shell "autoload" builtin - what is it good for?

查看:23
本文介绍了Z Shell“自动加载"内置 - 它有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Z shell 有一段时间了,我开始感到好奇.我在编写自己的函数时偶然发现的一件事是自动加载".

I have been using the Z shell for a while now, and I am starting to be curious. One thing I have stumbled at when writing my own functions is "autoload".

根据 zshbuiltins(1) 手册页 autoload 是等效于 functions -u"(有一个例外),即等效于 typeset-f"(有一个例外).但是,在查看了 autlooad 的使用后,例如 functions/Prompts/promptinit,我想我知道它的作用了.

According to the zshbuiltins(1) man page autoload is "equivalent to functions -u" (with an exception), which is "equivalent to typeset -f" (with an exception). However, after looking at the autlooad use of, say functions/Prompts/promptinit, I think I have an idea what it does.

我认为自动加载是一种导入"语句.

I think of autoload as, well, kind of "import" statement.

但是为什么autoload foo"优于source bar"?我不明白.

推荐答案

zsh 文档中所述:

一个函数可以被标记为未定义使用内置的自动加载(或functions -utypeset -fu).这样的函数没有主体.当...的时候函数首先执行,shell使用fpath 变量的元素.[...]

A function can be marked as undefined using the autoload builtin (or functions -u or typeset -fu). Such a function has no body. When the function is first executed, the shell searches for its definition using the elements of the fpath variable. [...]

autoload 允许在没有主体的情况下指定函数,然后在使用时自动加载;)

autoload allows for functions to be specified without a body which then get automatically loaded when used ;)

source 然而,将脚本作为参数,然后在当前会话的环境中执行 - 即您将保留所有更改脚本对环境有影响,而只是执行脚本时并非如此.

source however takes as argument a script which is then executed in the environment of the current session - i.e. you will retain all changes the script does to the environment, which is not the case when just executing the script.

我认为当函数中有大量实用程序时,此功能很有用.它允许更快启动(不需要加载自动加载函数的所有代码)并且可以保持shell的内存占用更小.

I think this feature is beneficial when having lots of utilities in functions. It allows for faster startup (all the code for the autoload functions need not be loaded) and may keep the memory footprint of the shell smaller.

这篇关于Z Shell“自动加载"内置 - 它有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆