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

Re: zmv exits from function




On 2024-01-02 06:51, Mark J. Reed wrote:

It's not parsed multiple times, but it _is parsed fully before anything runs.  _Parsing_ is just reading the code and determining what it means; actually _running_ it comes later. Zsh parses the whole _expression_ before it executes any part of it – it doesn't just stop and execute the first thing that looks like a command. So it has already read the `always` block and knows it's there before it executes the code to the left of it.
That's what I meant.  Right tho -- it's handled in two passes but one is called  parsing, the second is called running.   But the parse sets a memo that zmv will not be permitted to crash the function at run time.

And the presence of the `always` doesn't affect parsing; the code on both sides works the same way it would without the `always`. The only thing it does is tell Zsh that, as long as the code on the left doesn't exit the shell entirely, it doesn't matter what happens there; the code on the right needs to get executed next. Even if the left code interrupts the normal control flow with a `return` or `break` or `continue`, the code on the right will get executed before the next thing, wherever that next thing is.

This appears to be established practice, but I must say, seeing it for the first time, it sure looks bizarre.  It looks like some hideous hack to handle a code flow discontinuity that might have had a more straightforward handling:

    zmv ...   ...

    if [ $? -eq 0 ]; then echo "Files found, files moved boss.";  fi

    if [ $? -eq 1 ]; then echo "Ooops, no files found, but no problemo, keep calm and carry on.";  fi

    or:

    if [ $? -eq 1 ]; then echo "Ooops, no files found so this function won't have any more work to do so just return."; return;  fi

    or:

    if [ $? -eq 1 ]; then echo "WARNING!, no files found!!  This is a major problem so crash the entire shell."; exit;  fi

... You guys know best however.  But everything I've learned prior to this issue would have me expect that if zmv can't do anything it simply returns and code flow continues.  I wonder why it can't be just that simple.  It could very well be that other zsh functions  have this 'crash the calling function' property but it's the first time I've seen it.  (and zmv is soooo wonderful!)







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