When you are working with Perl scripts, you need to set permissions on the *.pl and *.cgi files so they are executable. Your FTP client will enable you to do this. Right-click on the file name and look for a submenu item “permissions” or similar. You will usually see a matrix of checkboxes for Owner, Group, and Public under the headings Executable, Read, and Write.
For executable *.pl and *.cgi scripts, use 755 (rwxr-wr-w). For supporting libraries that aren’t executable (scripts that you “require”), then the default upload of 644 (rw-r–r–) or 664 (rw-rw-r–) is usually fine. If you are creating a directory that needs to be writeable (for uploads or for program created files) then 666 (rw-rw-rw-) will usually do the trick, though you may be forced to use 766 (rwxrw-rw-).
If you are creating these directories with your Perl script, then you will need to change permissions when you create the directory.
my $dirname = "/system/path/to/foo"; mkdir $dirname; chmod 0766, $dirname;
A lot of documentation shows that you can set the permissions when you make the directory
mkdir $dirname, 0766; however, I recommend you don’t do that. If
umask(2) is being used on the system, you may wind up with a permission that is not what you intended. When you use
mkdir without the permission, then it defaults to
0777. Then you can change that with
umask(2) is implemented, then you may wind up not being able to do anything with the file (yes, it’s happened to me).
If you are working on a system where scripts are being uploded by different users, that is, with different FTP accounts, then you may run into a problem. When a file is uploaded, its ownership is set to the user that uploaded it. If one user uploads a file (normynewguy) and another user (siteowner) tries to access it with a Perl script they uploaded, then you might run into problems. The user “siteowner” could probably read it, but not write to it.
The way around this is that everyone changes ownership of files they upload so that the “siteowner” user can manipulate the files/directory:
my $uid = 12345; my $gid = 12345; my $dirname = "/system/path/to/foo"; mkdir $dirname; chown $uid, $gid, $dirname; chmod 0766, $dirname;
The UID (user id) and GID (group id) need to be numbers, not names. If you only know the name (your FTP username) then you need to add the following:
my $uid = getpwname('normynewguy'); my $gid = $uid; my $dirname = "/system/path/to/foo"; mkdir $dirname; chown $uid, $gid, $dirname; chmod 0766, $dirname;
NOTE: This example is for a shared hosting environment where the groupid will be the same as the userid. If you are on a dedicated server or in a large production environment, find out what your groupname is from the sysadmin and then use my
$gid = getpwname($namegiventome);
Now go, code, be happy. 🤓