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

Re: zsh dumping core because I don't grok TRAPEXIT



On Sep 8,  2:13am, Sweth Chandramouli wrote:
}
} 	OK.  The more I think about it, though, the less I see
} when TRAPEXIT would really be useful.  How would
} 
} function TRAPEXIT {
}    do_something
} }
} do_other_thing
} 
} 	ever be different than
} 
} do_other_thing ; do_something

When do_other_thing is a shell function, and do_something wants to use
the local variables of that function.

Also, it's an encapsulation issue: inside do_other_thing may be the best
place to control which do_something to do.

Finally, and a point I forgot to mention:  TRAPEXIT preserves $?, so
it's actually like writing

	do_other_thing; x=$? ; do_something ; return $x

For example:

	getpass() {
	    TRAPEXIT() { stty echo }
	    stty -echo
	    read passwd || return 1
	    print
	}

Without the trap, you'd at the least have to write something like

	getpass() {
	    stty -echo
	    read passwd || { stty echo ; return 1 }
	    print
	    stty echo
	}

Note that you can also do

	TRAPINT TRAPQUIT TRAPEXIT () { stty echo }

to create three traps at once, although unless you `setopt localtraps'
only the TRAPEXIT will automatically remove itself.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



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