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

Fwd: Surprising parsing result with anonymous functions and for loops



I meant to reply to the list, but only replied to Mikael instead.

---------- Forwarded message ----------
From: Clint Hepner <clint.hepner@xxxxxxxxx>
Date: Wed, Sep 24, 2014 at 3:11 PM
Subject: Re: Surprising parsing result with anonymous functions and for
loops
To: Mikael Magnusson <mikachu@xxxxxxxxx>


The POSIX standard defines a function definition as

    <name>  () <compound-statement>

where { ...; } is just one kind of compound statement, along with for
loops, while loops, etc. The following are all valid function definitions:

    foo () { echo bar; }
    foo () while [[ $i != foo ]]; do i=foo; done
    foo () for i in 1 2 3; do echo $i; done
    foo () ( echo bar; )
    foo () (( x=3 ))

The man page is ambiguous about what constitutes a function; it lists three
allowable forms:

       function word ... [ () ] [ term ] { list }
       word ... () [ term ] { list }
       word ... () [ term ] command

where the third is the one that describes the observed behavior.  I thought
the first might imply
special behavior when using the function keyword, but

    function foo (( x = 3))

works as well. The second is would seem to be a special case of the third,
except it does seem to treat the braces as part of the syntax:

    foo () { echo foo }

would in POSIX require a semicolon prior to the closing brace, but works in
zsh.

On Wed, Sep 24, 2014 at 10:07 AM, Mikael Magnusson <mikachu@xxxxxxxxx>
wrote:

> The intended command was something along these lines:
> () { for a { echo $a } } some words here
> but I forgot the enclosing { } and wrote the following
> () for a { echo $a } some words here
> surely this doesn't work, right?... wrong:
> % () for a { echo $a } some words here
> some
> words
> here
>
> Perhaps even more surprising is the following:
> % () for a { echo $a } ls
> ls
> --color=auto
> -T
> 0
> -A
> -v
> --quoting-style=shell
>
> I haven't looked at the parsing for the anonymous function stuff, but
> if it's not too hairy to fix, my vote is we drop this easter egg at
> some point.
>
> --
> Mikael Magnusson
>


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