zsh-users
 help / color / mirror / code / Atom feed
* limit scope of variable
@ 2021-08-03 20:15 Ray Andrews
  2021-08-03 20:39 ` Bart Schaefer
  2021-08-03 20:41 ` Vin Shelton
  0 siblings, 2 replies; 5+ messages in thread
From: Ray Andrews @ 2021-08-03 20:15 UTC (permalink / raw)
  To: Zsh Users

I have a function that requires this:

     local IFS=$'\n'

... but the function calls other functions in which I need to protect 
$IFS from that change.  Can I limit the scope?  As it is I'm laboring it 
with this:

In the calling function:

      local OLDIFS="$IFS"
      local IFS=$'\n'

In the called function:

     IFS=$OLDIFS

     code ...
     code ...

     IFS=$'\n'
     return

... there's got to be a more streamlined way.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: limit scope of variable
  2021-08-03 20:15 limit scope of variable Ray Andrews
@ 2021-08-03 20:39 ` Bart Schaefer
  2021-08-04  0:09   ` Ray Andrews
  2021-08-03 20:41 ` Vin Shelton
  1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2021-08-03 20:39 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

On Tue, Aug 3, 2021 at 1:15 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> I have a function that requires this:
>
>      local IFS=$'\n'
>
> ... but the function calls other functions in which I need to protect
> $IFS from that change.  Can I limit the scope?

That's what the zsh/param/private module is for, but unfortunately,
because IFS is a special parameter, you can't declare it private.

You can, however, declare IFS local in both functions, to avoid having
to explicitly restore it in the "inner" function.

calling_function() {
  local OLDIFS=$IFS
  local IFS=$'\n'
  called_function
}
called_function() {
  local IFS=$OLDIFS
  ...
}

However, it seems odd to me that called_function cares about the
$OLDIFS value.  I would think it either does not care, or needs to
declare its own value explicitly.  That is, I would think you don't
need $OLDIFS, and instead can do something like

called_function() {
  local IFS; unset IFS # use default
  ...
}


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: limit scope of variable
  2021-08-03 20:15 limit scope of variable Ray Andrews
  2021-08-03 20:39 ` Bart Schaefer
@ 2021-08-03 20:41 ` Vin Shelton
  2021-08-03 20:44   ` Bart Schaefer
  1 sibling, 1 reply; 5+ messages in thread
From: Vin Shelton @ 2021-08-03 20:41 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 788 bytes --]

I think you can prefix the called function with IFS=$OLDIFS.  I.e.:

IFS=$OLDIFS Rays_function

E.g.:

function a {
; echo $a
; }
: ~ Tue 3 16:37; a=foo
: ~ Tue 3 16:37; a
foo
: ~ Tue 3 16:37; a=bar a
bar
: ~ Tue 3 16:37; a
foo


On Tue, Aug 3, 2021 at 4:15 PM Ray Andrews <rayandrews@eastlink.ca> wrote:

> I have a function that requires this:
>
>      local IFS=$'\n'
>
> ... but the function calls other functions in which I need to protect
> $IFS from that change.  Can I limit the scope?  As it is I'm laboring it
> with this:
>
> In the calling function:
>
>       local OLDIFS="$IFS"
>       local IFS=$'\n'
>
> In the called function:
>
>      IFS=$OLDIFS
>
>      code ...
>      code ...
>
>      IFS=$'\n'
>      return
>
> ... there's got to be a more streamlined way.
>
>
>

[-- Attachment #2: Type: text/html, Size: 2080 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: limit scope of variable
  2021-08-03 20:41 ` Vin Shelton
@ 2021-08-03 20:44   ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2021-08-03 20:44 UTC (permalink / raw)
  To: Vin Shelton; +Cc: Ray Andrews, Zsh Users

On Tue, Aug 3, 2021 at 1:41 PM Vin Shelton <acs@alumni.princeton.edu> wrote:
>
> I think you can prefix the called function with IFS=$OLDIFS.  I.e.:
>
> IFS=$OLDIFS Rays_function

Yes, but that has the possibly-unwanted side effect of exporting IFS
into the environment for everything run from within Rays_function.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: limit scope of variable
  2021-08-03 20:39 ` Bart Schaefer
@ 2021-08-04  0:09   ` Ray Andrews
  0 siblings, 0 replies; 5+ messages in thread
From: Ray Andrews @ 2021-08-04  0:09 UTC (permalink / raw)
  To: zsh-users

On 2021-08-03 1:39 p.m., Bart Schaefer wrote:
>
> called_function() {
>    local IFS; unset IFS # use default
>    ...
> }
>
That's all it takes.  Indeed I just wanted the default but I didn't know 
I could just unset the variable.  Sounds like the sort of thing that 
might cause some sort of deep breakage like erasing your $PATH or 
something.  Anyway the unset seems fine, thanks Bart and Vin. Probably 
if I was better I could avoid tinkering with IFS at all.




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-08-04  0:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-03 20:15 limit scope of variable Ray Andrews
2021-08-03 20:39 ` Bart Schaefer
2021-08-04  0:09   ` Ray Andrews
2021-08-03 20:41 ` Vin Shelton
2021-08-03 20:44   ` Bart Schaefer

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).