Making and Removing Directories
Making a directory inside an existing directory is easy. Just
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[*] 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 but no
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 (either a literal or a calculation). But unless you can
quickly can figure that 0755 octal is
493 decimal in your head, it’s
probably 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 you saw earlier (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 ...