(Note that this is less for
tilly's benefit than it is for any casual user that is trying to take advantage of this trick.) Unfortunately
$! and system return values have
nothing in common. You can (and must) never assume that an arbitrary program's return code would or should ever map to any standard error message (such as via
$!). However, if you are making use of some external program that
does define its return code behavior in this fashion (such as something you write specifically to this criteria), by all means give it a shot.
Setting the value of $! is also useful for system-"like" functions of your own design, but generally setting this variable is only ever useful when your system-like function is doing some core-level operation that itself fails, in which case $! should already be set. Just don't change it when you return.