From $! in perlvar:
in reply to Strange behaviour: $! is set after successful socketpair() call
Many system or library calls set errno if they fail, to indicate the cause of failure. They usually do not set errno to zero if they succeed. This means errno , hence $! , is meaningful only immediately after a failure
You can't use $! as an indicator of whether a call failed. I am not particularly familiar with what you are attempting, but assuming using $! to check success on modifying $) is appropriate, I would probably implement that bit as:
This brings it more in line with the principle of least surprise, since the funky checks are all localized to the block. This could also function using localization or explicit undef.
my $cache = $!;
$) = $run_gid;
die "Setting egid: $!" if $! and $! ne $cache;
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.