miyagawa/CGI-Compile — pull request #12 by tfoertsch (2014-05-07 10:11:56+00:00)
Title: Various fixes

# Changes #

## Fix `%SIG` localization ##

Previously, `%SIG` was localized by `local *SIG=+{%SIG}`. Although that localizes the hash, its values loose their magic. Hence, setting `$SIG{$sig}=\&handler` does not work anymore as expected.

This change localizes the values instead of the hash itself like `local @SIG{keys%SIG}=values %SIG`.

## Fix exit code handling inconsistencies ##

Previously, if the script simply ended without calling `exit`, the generated sub returned the result of the last statement of the script. However, if the script called `exit	`, the sub returned `undef` on `exit 0` or died with a `exited nonzero: ...` message. This is inconsistent. `exit $rc` or simply `$rc` at the end of the script should behave identical.

Solutions might be:

1. never `die`, even if the script itself dies on something else than `exit`. That would be most consistent with the operating system shell. But the shell does not know about Perl exceptions.
2. propagate exceptions other than `exit` and return the exit value no matter if it's `0` or else.
3. propagate exceptions other than `exit` and throw an exception if the script's exit status is something other than `0`. Return normal only if the script returns or exits with `0`. Here an additional decision that has to be made is whether `''` or `undef` are taken as `0` as the shell would do or not.

The proposed change implements 2. and converts the script's return/exit code to a number. So, `''` and `undef` are taken as `0`.

## Fix variable exposure ##

Previously, lexical variables in `compile()` were exposed to the script and the variable names were not very obfuscated. So, clashes with not well-written scripts could happen. This is fixed.

# Tested on #

* debian wheezy with the system's Perl 5.14.2
* a self-built Perl 5.18.0 on openSuse 12.2