Perl oct Function

These sites are FOR SALE: miscperlinfo.com, mpihowto.com!
If you are interested, please use my Contact page.




This short Perl oct Tutorial will show you how to use this function in your scripts. This 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 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 of the Perl oct function looks like this:

oct EXPR
oct



Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!


After conversion, the Perl 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.

Simple examples


Here’re a few examples of how you can utilize this 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

Octal File Permisions and the Perl oct function


You can change the permissions of a list of files by using the Perl 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 644 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)

Please click here to download the Perl oct script with all the above examples included.

A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!


Check these how-to tutorial eBooks (PDF format):


Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        delete
        each
        grep
        hex
        index
        join
        lc
        lcfirst
        length
        map
        oct (more)
        ord
        pack
        pop
        push
        q
        qq
        qw
        reverse
        rindex
        scalar
        shift
        sort
        splice
        split
        sprintf
        substr
        tr
        uc
        ucfirst
        undef
        unshift

return from Perl oct function to Perl Basics



Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!