in reply to Returning from a program

Why not try exit($retval) where $retval has been set to the
value you wish to return as the error/success status

Hope this helps