zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: zsh-users@zsh.org
Subject: simple question regarding local variables
Date: Sat, 19 Sep 2015 09:23:53 -0700	[thread overview]
Message-ID: <55FD8C19.4050602@eastlink.ca> (raw)
In-Reply-To: <150918145300.ZM27125@torch.brasslantern.com>

I should know this:

Say several functions will use the same set of variables, so you want
some way of auto creating them for each function, *but* they must be
local to each function.

This works:

'test1' is a script:

       #!/usr/bin/zsh
       local variable=howdy
       local anothervariable=whatever

    local foo=FOO
    local bar=BAR
    local baz=BAZ
    # and so on ... I create all my variables in the script.


'test2' is a function that sources 'test1':

    test2 ()
    {
       varis variable
       .  ./test1
       varis variable
    }


('varis' simply prints the name and the value of variables.)

A run:

    $ test2; varis variable
    $variable is: ""
    $variable is: "howdy"
    $variable is: ""


... just as I want, sourcing 'test1' creates local variables in my 
function, but why does it
work?  sourcing I understand to create a child shell, thus how is it 
that after 'test1'
returns, that it's local variables are known to 'test2'?  Exactly as I 
want, '$variable'
evaporates after 'test2' returns, so it's local.  I like it, but I can't 
explain it.




  reply	other threads:[~2015-09-19 16:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-18  6:34 Autoload vs regular function Sebastian Gniazdowski
2015-09-18 17:15 ` Bart Schaefer
2015-09-18 18:45   ` Ray Andrews
2015-09-18 21:53     ` Bart Schaefer
2015-09-19 16:23       ` Ray Andrews [this message]
2015-09-19 16:32         ` simple question regarding local variables Bart Schaefer
2015-09-19 18:21           ` 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=55FD8C19.4050602@eastlink.ca \
    --to=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).