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

Re: sourcing a sh file in zsh



On 2009-01-25 at 13:41 +0300, Andrey Borzenkov wrote:
> I asked already - why would you want to create non-zsh function inline 
> in zsh script?

Unit tests.

If I, or anybody else, is going to make changes for which bugs could
crop up far and wide if I/we make mistakes, it will have unit tests
written.

Writing those tests is much easier if you can include the tests inline.

Now, those tests could be arranged to end up in external files before
being run, the most obvious mechanism being (assuming -S for Sticky):
  fn==(cat <<-\HERE
	# shell bits
	HERE
	) && emulate -SR sh -c "source $fn"

but being purely inline would be less grotesque to read.

I see the setopt/emulate argument and I can see benefits either way.  I
went with setopt just because it was what I thought of first and it
seems easiest, but that doesn't mean it's best.  What I do like with the
setopt approach is that there's a way for zsh to test if this is
currently the case.

Eg, for bash/zsh portability, you could do:
  if [[ -n $ZSH_VERSION && -o sticky_options ]]
since both support [[ conditional ]] and both support -o as a unary
prefix test (both using it for testing shell options).

-Phil



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