zsh-users
 help / color / mirror / code / Atom feed
From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: "Lawrence Velázquez" <larryv@zsh.org>
Cc: linuxtechguy@gmail.com, zsh-users@zsh.org
Subject: Re: anonymous function question
Date: Tue, 23 Aug 2022 09:21:27 +0200	[thread overview]
Message-ID: <CAN=4vMrdEGcefDr+HbnQx=KphB9EomMfJpKp5y2JemqePtsEHQ@mail.gmail.com> (raw)
In-Reply-To: <4f73130d-1d7b-46af-ab04-72a04fc89727@www.fastmail.com>

On Tue, Aug 23, 2022 at 5:37 AM Lawrence Velázquez <larryv@zsh.org> wrote:
>
> - Comment removal is performed during parsing.  Thus, setting and
>   unsetting INTERACTIVE_COMMENTS changes how parsing is done.
> - A complex command is parsed *in its entirety* before *any* of its
>   commands are executed.  Thus, a command within a complex command
>   cannot affect how the rest of the complex command is parsed.
>
> Therefore, a complex command cannot enable or disable comments
> within itself.  It can affect subsequent commands, though:

That's basically correct. Note that *all* options stay constant during
parsing, not just INTERACTIVE_COMMENTS. Also note that functions are
always parsed in their entirety and only once and aliases are expanded
during parsing. This often surprises people. Consider this script:

    alias foo='echo 1'

    function bar() {
      foo
      alias foo='echo 2'
    }

    bar
    bar
    bar
    foo

This script prints "1" three times followed by "2". If you add
`functions bar` at the bottom to print the body of function bar, it'll
reveal what's going on:

    bar () {
      echo 1
      alias foo='echo 2'
    }

As you can see, the alias within the function has been expanded during parsing.

There are several more options (in addition to INTERACTIVE_COMMENTS
and ALIASES) that affect parsing: SH_GLOB, BRACE_EXPAND, etc.

    # Set unusual options.
    setopt no_brace_expand

    function foo() {
      # Set the usual options.
      emulate -L zsh
      print -- {1,2}
    }

    foo

This prints "{1,2}" rather than "1 2". Options set by a function
affect its execution but not parsing.

Another thing that can be surprising is that zcompile parses the whole
file all at once with the options and aliases that are active at the
time zcompile is invoked. This means that sourcing a file directly may
have a different effect from zcompiling and sourcing it afterwards.

    >script.zsh <<\END
    alias echo='echo 1'
    echo 2
    END

    ( source ./script.zsh )
    zcompile ./script.zsh
    ( source ./script.zsh )

This prints "1 2" followed by "2".

This is one of the reasons I wouldn't recommend zcompiling zsh startup
files. There are other (and more important) reasons, too.


Roman.


      parent reply	other threads:[~2022-08-23  7:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-23  2:11 Jim
2022-08-23  3:36 ` Lawrence Velázquez
2022-08-23  4:54   ` Jim
2022-08-23  7:21   ` Roman Perepelitsa [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='CAN=4vMrdEGcefDr+HbnQx=KphB9EomMfJpKp5y2JemqePtsEHQ@mail.gmail.com' \
    --to=roman.perepelitsa@gmail.com \
    --cc=larryv@zsh.org \
    --cc=linuxtechguy@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).