in reply to state and file perms confusion
Because the mode contains both the file type and its permissions, you
+should mask off the file type portion and (s)printf using a "%o" if y
+ou want to see the real permissions.
$mode = (stat($filename));
printf "Permissions are %04o\n", $mode & 07777;
You've masked off the file type with $mode = $mode & 07777;, so you'll get 0644 (octal) from the script above when your $mode equals 420 (decimal), since the printf above makes the conversion from decimal to octal.