Perl oct Function

The oct function is used to convert an octal string into the numerical corresponding value.

1. The syntax forms of the oct function

The oct function is used to interpret an expression as an octal string and to return the corresponding decimal value. To get the reverse, i.e. to produce a number in octal you can use the sprintf or printf functions.

An octal number is preceded by a 0 and contains digits from 0 to 7 only. By calling the Perl oct function, there are two particular cases here – the expression will be interpreted as a hexadecimal string if begins wit 0x and as a binary string if it begins with 0b.

If you want to convert numbers in or from a binary, octal or hexadecimal format you need to write them as literals, otherwise no automatic conversion will take place. The leading white spaces from the expression string will be omitted.

The syntax forms of the oct function look like this:

oct EXPR
oct
After conversion, the oct function returns the corresponding decimal value of the EXPR parameter as a string. The string returned by this function can be used as a number because Perl automatically converts the strings to numbers in a numerical context, by using the decimal base.

If the expression is omitted, the \$_ is used instead.

Please notice that the Perl oct function can convert a binary, octal or hexadecimal number into a decimal format, whereas the hex function converts only hexadecimal numbers, with or without the prepended 0x.

2. How to use the oct function - a few examples

Here there are a few examples of how you can utilize the oct function:

#!/usr/local/bin/perl

use strict;
use warnings;

print oct(77), "\n";        # it prints 63

print oct('77'), "\n";      # it prints 63

print oct('077'), "\n";     # it prints 63

print oct(077), "\n";       # it prints 51
# because 077 is an octal number, first it will
# be converted to a decimal number, i.e. 63 and next
# oct(63) means 51

print oct('2382'), "\n";    # it prints 19
# this statement will throw a warning error
# because 8 is not a digit in the octal base; however,
# the rest of digits starting with 8 will be ignored and the oct
# function will convert the octal number 23 in decimal

print oct('   0x77'), "\n";    # it prints 119
# '   0x77' is considered the hexadecimal number 77;
# converted in decimal this means 119;
# the leading spaces are ignored

print oct('0b101'), "\n";   # it prints 5
# '0b101' is considered the binary number 101;
# converted in decimal this means 5

3. Octal File Permissions and the oct function

You can change the permissions of a list of files by using the chmod function. This function expects a file permission as its first argument and that needs to be in octal. You can either provide a leading 0 for the value of the argument or use the Perl oct function.

For example, if you want to assign the 664 permission to a file (which means that owner can read and write and other people only read) you can use any of the two following lines of code:

chmod(0664, \$file);
chmod(oct(664), \$file);

where \$file is the handle of the appropriate file. Using the oct function, you don’t need to prepend the permission number with a 0 digit.

In the following, I remind you how to use the octal syntax notation in connection with the file permission.

The first digit of the octal number 664 is for the owner, the second for the group and the last for others. The permission could be r (for reading), w (for writing), x (for executing) and (no permission).

Each digit of the permission number can have one of the following values:

 value 0 1 2 3 4 5 6 7 permission --- --x -w- -wx r-- r-x rw- rwx

So 664 means the following permissions:

rw-rw-r-- (the owner and the group can read and write, the other people only read)