As requested, here's the same test case a little more readable:
This leaves a backdoor open (possibly in the saved UID):
======================================
#!/usr/bin/perl -wT
use strict;
use English qw(-no_match_vars);
sub ids { print "RUID=$REAL_USER_ID EUID=$EFFECTIVE_USER_ID\n" }
ids;
$REAL_USER_ID = 1000;
$EFFECTIVE_USER_ID = 1000;
ids;
$REAL_USER_ID = $EFFECTIVE_USER_ID = 0;
ids;
======================================
OUTPUT:
RUID=1000 EUID=0
RUID=1000 EUID=1000
RUID=0 EUID=0
Still, changing the order of the "*_USER_ID = 1000" lines or using
POSIX::setuid(1000) works as expected.
Christopher