you cannot use $!,
1) It is only meaningful, if you check it right after the failure. However you are tryinh to check it after a functuion returned, and at that moment, $! could be changed long time ago;
2) $! is usually set by system or library calls.
However this does NOT mean $! is readonly, you can change it. If you want it to be meaningful, set it to a number outside the range that being used by system. Again, there is a big chance that, at the time you check its value, it has been reset by the system, so you don't get what you want.