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

(some tips about variables) Re: avoid eval?



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



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