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

REPLY retains its integer attribute in glob qualifier functions



For
https://unix.stackexchange.com/questions/450207/how-to-sort-directories-using-permissions/450211#450211

I was defining a glob qualifier sorting function as:

zmodload zsh/stat
byperm() {
  stat -LA REPLY +mode -- "$REPLY" &&
    ((REPLY = REPLY & 07777))
}

echo *(no+byperm)

And found that it was misbehaving.

$ ls
a.txt  b.txt
$ echo *(o+byperm)
zsh: bad floating point constant

It turns out that ((REPLY = REPLY & 07777)) gives the "integer"
attribute to REPLY and it somehow fails when $REPLY is assigned
the next file name.

Note that it only happens if REPLY is first made an array (or
associative array or is unset).

This is OK:

$ unset REPLY; f() ((REPLY = 1)); echo *(o+f)
b.txt a.txt

(REPLY is not getting the integer attribute upon ((REPLY = 1)),
because it was already set as a non-integer scalar)

This is not:

$ unset REPLY; f() {REPLY=(); ((REPLY = 1))}; echo *(o+f)
zsh: bad floating point constant
a.txt b.txt
$ typeset -p REPLY
typeset -i REPLY=0

This time the ((REPLY = 1)) creates REPLY as a new scalar so
gets the integer attribute. Whatever attributes were given by
the function are retained for when zsh prepares $REPLY with the
next file as *input* to the next invocation of the function.

It seems to me zsh should reset the variable to a plain scalar
before assigning to the file names as input to the function.

-- 
Stephane



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