Udemy logo

perlstringfunctionsPERL stands for Practical Extraction and Report Language. It’s a programming language which runs on different platforms like Windows, Unix and Macintosh. C or Unix shell developers will find it easy to migrate to this platform. It’s an open source software and is known for its stability. The current version of Perl is  5.16.2. Perl provides support for both procedural and object-oriented programs. It’s easily able to connect to databases such as Oracle, Sybase, MySQL and more. This language is convenient to use with markup languages such as HTML, XML and others. Perl offer support for Unicode and is Y2K compliant. Perl is quite easy to learn, even if you’re new to programming. Take this introductory Perl course to see for yourself.

Today we walk you through the Perl change mode command. This command is used to set permissions to the directories and files. We assume that you have the basic knowledge of programming. If not you can take this introductory course to programming to help get you started.

What is Chmod?

Chmod is an acronym for Change Mode. It comes as a set of three numbers. Each of these three numbers is calculated by adding three other numbers. This command is used for both file and directories. The syntax for this command is as follows.

chmod MODE, LIST

Here the Mode is the permissions and list is the file(s) whose permission is to set/changed according to the mode.

You can learn more about chmod in this Linux course (chmod in Perl and Linux work the same).

Modes of the Command Chmod

Operators for Chmod Command

These operators are used to set or remove the permissions of the files. They are as follows.

  1. The ‘+’ operator adds the specified modes.
  2. The ‘-‘ operator removes the specified modes.
  3. The “=” operator results in making the specified modes into the exact mode for the file.

Modes of the Chmod Command

The three basic modes in Perl are listed below. Take a look.

  1. ‘r’ stands for read permission for a file or directory’s content.
  2. ‘w’ stands for write permission for a file or directory.
  3. ‘x’ stands for execute permission for a file or recursive permission for a directory.

Special execute permission

‘X’ is used to set special execute permission to both the files and directories. It is usually used along with the -R option. For example, chmod -R a+X <dir name> Remove the execute permission for all files in a directory tree. However.it allows directory browsing.

The Numerical Values and the Various Combination of the Modes

The value of Read(r) is 4, Write(w) is 2 and Execute(x) is 1. The following table lists all the possible combinations which a file or directory can have using the three command types.

Digit                      rwx                        permission set


0                              —                           no access to the file

1                              –x                          The file has only execute permission

2                              -w-                         Only write permission is set

3                              -wx                        The file has both write and execute permission but no read permission.

4                              r–                           Only read permission is given.

5                              r-x                          read and execute permissions are given.

6                              rw-                         read and write permission is set for the file.

7                              rwx                        All the three permissions are set, read, write and execute.

Normally, for files the permission settings are 777, 755, 666 or 644. While for the directories the settings are 777 or 755. For CGI (Common Gateway Interface) scripts the settings are 755, the data files are set to 666 and the configuration files have the settings 644. The first number indicates the host , the second stands for the group, and the third number stands for the others, the visitors who visit the site.

Examples Illustrating the Use of Chmod Command

Example 1: Program to Call Chmod in Perl

    use strict;
    use warnings;
    my $filename = "example.txt";
    chmod 0600, $filename or die "Couldn't change the permission to $filename: $!";
    my $dir = "exampledir";
    chmod(0660, $dir) or die "Couldn't set the permission to $dir: $!";
    exit 0;

This program uses chmod function to set permissions to a file and directory. In case chmod doesn’t execute properly, an error message is displayed on the output screen. The first line of the above program is called the shebang line. It is used to make the Perl script portable to Unix and Cygwin. It is recommended to use the same even on Windows. ‘use strict’ and ‘ use warnings’ statements are used to tell the Perl interpreter to check for programming errors. The my keyword declares the listed variables to be local to the enclosing block. Die command is used to display a message for the user.

Example 2:  Program to Call Chmod on a List of Files

    use strict;
    use warnings;
    my @files = ("example1.txt", "example2,txt", "example3.txt");
    my $res = chmod 0600, @files;
    print "Modified $res files\n";
    die $! if ($!);
    exit 0;

In this program, we use the chmod command on three files. The variable res accepts the number of files successfully modified by the chmod command.

Hope this article helped you understand the  Perl chmod command better. Do experiment with these examples and try them out for yourself. You may also want to learn the basics of other scripting languages like bash or Python (you can learn with this course) to compare what works best for you.

Page Last Updated: June 2014

Featured course

Enhance Your Skill Set with Perl

Last Updated January 2020

Highest Rated
  • 16 total hours
  • 115 lectures
  • Beginner Level
4.6 (582)

See Perl work, Hear Perl explained, Practice Perl to be an even more Skilled, Versatile, and Valuable I.T. Professional | By Darryl Pace

Explore Course

Perl students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo