Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: limit scope of variable



On Tue, Aug 3, 2021 at 1:15 PM Ray Andrews <rayandrews@xxxxxxxxxxx> 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
  ...
}




Messages sorted by: Reverse Date, Date, Thread, Author