Making and Removing Directories
Making a directory inside an existing directory is easy. Invoke the mkdir function:
mkdir "fred", 0755 or warn "Cannot make fred directory: $!";
Again, true means success, and $! is set on failure.
But what’s that second parameter, 0755? That’s the initial permission setting[293] on the newly created directory (you can always change it later). The value here is specified as an octal value because the value will be interpreted as a Unix permission value, which has a meaning based on groups of three bits each, and octal values represent that nicely. Yes, even on Windows or MacPerl, you still need to know a little about Unix permissions values to use the mkdir function. Mode 0755 is a good one to use because it gives you full permission but lets everyone else have read access without permission to change anything.
The mkdir function doesn’t require you to specify this value in octal. It’s just looking for a numeric value (a literal or a calculation), but unless you can quickly figure that 0755 octal is 493 decimal in your head, it’s easier to let Perl calculate that. And if you accidentally leave off the leading zero, you get 755 decimal, which is 1363 octal, a strange permission combination indeed.
As we saw in Chapter 2, a string value being used as a number is never interpreted as octal even if it starts with a leading 0. So this doesn’t work:
my $name = "fred";
my $permissions = "0755"; # danger... this isn't working
mkdir $name, $permissions;Oops, we ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access