-rw-r--r--. If that looks familiar to you, skip this and the next paragraph.

OK, now that it's just you and me, Bill Gates, it is an example of Unix file permissions. The example given means that the user (owner) can read/write to the file, and others can only read it. Checking these permissions in Perl is simple; just use the stat method to check its mode (the file type and permissions). For example, if you wanted to check read permissions, you'd check bits 4 (owner read), 32 (group read), and 256 (other read). This is 292 in decimal, 0444 in octal. Compare that to the mode, and you'll know whether you can read the file. In code, stat($path)->mode & 0444. Easy peasy!

Ed's colleague hated built-in functions. Why trust mode when you can roll your own permission checking algorithm?

my $perms = stat($path);
my $permsbin = unpack("B32", pack("N",$perms->mode & 0777));
$permsbin =~ s/.*(.)..(.)..(.)../$1$2$3/;
if ($permsbin != 111) {
   # file is not readable
   # ...

So first, the value is packed into a 32 bit unsigned long (that's what the pack("N") means), then he converts it into a string of bits. That is, literally, a string of ones and zeros. Then he uses a regex to parse out the specific bits in the read positions for owner, group, and other, and turns it into its own string of bits, hoping the resulting string is "111". Finally, he has Perl compare "111" to 111, forcing his new string to be magically converted into an integer. Clearly, this guy is a fan of Rube Goldberg machines.

You know the famous quote about regular expressions?

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. — Jamie Zawinski

I think it's safe to say that Ed's colleague has more than two problems in his oft-copied-and-pasted permissions checking routine.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!