zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: TJ Luoma <luomat@gmail.com>
Cc: Zsh-Users List <zsh-users@zsh.org>
Subject: Re: How do you decide whether to make something a function or a script?
Date: Sat, 8 Sep 2018 20:24:13 -0700	[thread overview]
Message-ID: <CAH+w=7ahvysYEc__cG8v2UPYPAWDZiuLP6dbmZoMrXKW1EdNKA@mail.gmail.com> (raw)
In-Reply-To: <CADjGqHuK2oV3VzjHO0H5+FHJXC85HmysL9yPRjS-ESLhBcC3FA@mail.gmail.com>

On Sat, Sep 8, 2018 at 6:22 PM, TJ Luoma <luomat@gmail.com> wrote:
>
> Why make any functions at all? Why not just make them all scripts?

The primary reason to make a program into a function is when it needs
to alter the state of the parent shell.  User-defined ZLE widgets are
the most obvious example, but there are other cases to want to make
persistent changes to the current shell.  (Yes, you can get some of
this with "source", but then you have to worry about parameter
scoping, etc.)

A secondary reason is efficiency.  Running a script requires forking a
subshell, examining the #! line to see if an external interpreter is
needed, and parsing the code, all before the script can begin doing
anything, and this happens every time it is run.  With functions,
there's no subshell, no guessing at the interpreter, and all the
parsing has been done up front (or is done only once on the first run,
in the case of an autoload).

Another reason is modularity.  A single file can define several
related functions, which leverages both of the first two advantages.

Yet another reason is to facilitate recursive programming.  A script
that runs itself creates many subshells, but a function that calls
itself only uses some recoverable memory space in the current shell.

There are more reasons, but most of them begin to be stylistic rather
than practical.  I'm sure others can add to my list.

      parent reply	other threads:[~2018-09-09  3:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-09  1:22 TJ Luoma
2018-09-09  1:55 ` Grant Taylor
2018-09-09  2:21   ` Daniel Shahaf
2018-09-09  2:51     ` Grant Taylor
2018-09-09 19:34     ` TJ Luoma
2018-09-09 22:54       ` Bart Schaefer
2018-09-09  3:24 ` Bart Schaefer [this message]

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='CAH+w=7ahvysYEc__cG8v2UPYPAWDZiuLP6dbmZoMrXKW1EdNKA@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=luomat@gmail.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).