zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: local but persistent integer?
Date: Fri, 16 Sep 2022 10:44:59 -0700	[thread overview]
Message-ID: <CAH+w=7bxYiss1853OBFsKzxAOYV66XnB5QeZUi=pDWp=q0==0g@mail.gmail.com> (raw)
In-Reply-To: <22a10f61-5f82-9797-4bc9-9082f9371dc7@eastlink.ca>

On Fri, Sep 16, 2022 at 8:19 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
>          integer -x start="$(( $(date +%s%N)/1000000 ))"
>
> ... I need 'start' to persist between function calls which it does

Given that construct, it should not.  "integer -x" within a function
puts something in the environment (so visible to external processes),
but it's still scoped to the function and goes away when the function
finishes.

> I tried 'unset' but it didn't seem to work.

That indicates that you're unsetting the local, but there must still
be another global $start that came from somewhere.  Use a more
distinctive variable name (see below).

> it would be nice if it could be local as well since it has no use
> outside this function.

Unfortunately the base model for shell variables (a form of dynamic
scoping) and interpretive function invocation does not lend itself to
the form of static scoping that you're hoping for.  A second run of a
function is just re-interpreting the function source code, not
re-entering any existing scope other than the global one.

Your best bet is to use something like

integer -gH _myfn_start=$((....))
(( $_myfn_start == 0 )) && ...

which will create a global variable whose value is concealed, using a
name that only your function "knows about".

This can be generalized with something like this (assumes proper
option settings to make ${0} reflect the current function name):

integer -gH _${0}_start=...
(( ${(P)${:-_${0}_start}} == 0 )) && ...

but that's probably overkill in your case.


  reply	other threads:[~2022-09-16 17:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-16 15:19 Ray Andrews
2022-09-16 17:44 ` Bart Schaefer [this message]
2022-09-16 19:43   ` Ray Andrews
2022-09-16 21:41     ` Bart Schaefer
2022-09-16 21:50       ` Ray Andrews
2022-09-17  1:51       ` Ray Andrews

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=7bxYiss1853OBFsKzxAOYV66XnB5QeZUi=pDWp=q0==0g@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=rayandrews@eastlink.ca \
    --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).