zsh-users
 help / color / mirror / code / Atom feed
From: Marc Chantreux <khatar@phear.org>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: zsh-users@zsh.org
Subject: (some tips about variables) Re: avoid eval?
Date: Tue, 13 Mar 2018 17:36:34 +0100	[thread overview]
Message-ID: <20180313163634.s4qvlfhdzqplxn4s@prometheus.u-strasbg.fr> (raw)
In-Reply-To: <e4c25a58-1089-cdba-3e70-d8cce39bbc8d@eastlink.ca>

hello,

> Is there any way to immunize my functions against $IFS trouble?  I have
> functions that seem to require " $IFS=$'\n' " and others that insist on "
> $IFS=' '  ".

you should be carreful to reduce the scope of the change of such an
important variable. more generally, you should localize every variables
of the functions using local.

also, you can make your functions more reliable by reporting when

* you are using unset variables
* you are setting a global variable in the functions

to summarize:

* use zsh options that protects you from mistakes

    setopt warncreateglobal nounset

* keep the IFS change as tight as possible by setting it for only one
  read. exemples

    getent passwd |
        while {IFS=: read login _ uid gid gecos home shell } {
            [[ $shell == *zsh* ]] && print $login is cool
        }

    slurp     () { IFS=$'\n' read -d '' -A $1 }
    readlines () { local _; IFS=$'\n' read -d '' "$@" _ }

* at least, localize your variables

    slurp     () { local IFS=$'\n'  ; read -d '' -A $1 }
    readlines () { local _IFS=$'\n' ; read -d '' "$@" _ }


regards,
marc


  parent reply	other threads:[~2018-03-13 16:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-11 20:48 Ray Andrews
2018-03-11 22:53 ` Marc Chantreux
2018-03-12  0:43   ` Ray Andrews
2018-03-13  2:13     ` Ray Andrews
2018-03-13  2:17       ` Eric Cook
2018-03-13 16:36       ` Marc Chantreux [this message]
2018-03-13 17:50         ` (some tips about variables) " Ray Andrews
2018-03-13 20:39           ` Marc Chantreux
2018-03-13 21:50             ` 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=20180313163634.s4qvlfhdzqplxn4s@prometheus.u-strasbg.fr \
    --to=khatar@phear.org \
    --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).