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

Passing arguments to anonymous functions



Zsh has had anonymous functions for a couple of years now:

% (){ print hello world }
hello world
% 

These are useful for things like creating local variable scopes, which
you can't do with a simple { ... } expression.  However, $@ and $* are
always empty in an anonymous function, which makes them somewhat less
useful than a named function which can be called with arguments.

However, you can redirect the input of an anonymous function, and you
can read into the positional parameters:  

% (){ read -A argv; print -l $@ } <<<"goodnight gracie"
goodnight
gracie
% 

This isn't quite as nice as one might like since one must supply a
single shell word to follow the <<< redirection, but for simple cases
it does suffice, and one can imagine all sorts of tricks involving e.g.
IFS=$'\0' (if one does not use the new POSIX_STRINGS option) to allow
parsing of arguments passed from caller to callee.



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