Function oct() in Perl

In the Perl programming language has a built-in function oct().

Function oct() converts a number from octal number system to decimal. But even this function can convert a number in decimal number system and from binary and hexadecimal.

Here's an example:

▶ Run
#!/usr/bin/perl

use feature qw(say);

say oct('10'); # 8
say oct('0x10'); # 16
say oct('0b10'); # 2

Arguments

If the function oct not given no arguments, the function works with default variable $_:

▶ Run
#!/usr/bin/perl

$_ = 123;

print oct(); # 83

In that case if the variable $_ is undef and used use warnings;, will warning:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $dec = oct();
Use of uninitialized value $_ in oct at script.pl line 6.

Standard using oct() is to pass it a single argument.

If the transfer function oct() more than one argument, it will be an error and code execution will be stopped.

Too many arguments for oct at script.pl line 3, near "2)"
Execution of script.pl aborted due to compilation errors.

If you need to convert multiple values, you can use map:

▶ Run
#!/usr/bin/perl

use Data::Dumper;

my @oct = (100, 200, 300);

my @dec = map { oct } @oct;

warn Dumper \@dec;

If the function is passed a string that starts with 0x or 0X, the function oct() interprets this string as a number in hexadecimal notation and returns this number is in decimal.

But if the string starts with 0b or OB, then ord() considers the line number in binary notation and returns its decimal representation.

If in oct() pass number in which there is a character that is not included in the range of numbers from 0 to 7, then oct() will return 0, but included use warnings; will warning:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use feature qw(say);

say oct(90210);
Illegal octal digit '9' ignored at script.pl line 7.
0

Function oct() can not work with fractional values:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use feature qw(say);

say oct('10.7');
say oct('0X10.F');
say oct('0B10.11');
8
16
2
Illegal hexadecimal digit '.' ignored at script.pl line 8.
Illegal binary digit '.' ignored at script.pl line 9.

Return value

Function oct() always returns a number.

Official documentation

Here is the output of the command perldoc -f oct:

    oct EXPR
    oct     Interprets EXPR as an octal string and returns the corresponding
            value. (If EXPR happens to start off with "0x", interprets it as
            a hex string. If EXPR starts off with "0b", it is interpreted as
            a binary string. Leading whitespace is ignored in all three
            cases.) The following will handle decimal, binary, octal, and
            hex in standard Perl notation:

                $val = oct($val) if $val =~ /^0/;

            If EXPR is omitted, uses $_. To go the other way (produce a
            number in octal), use "sprintf" or "printf":

                my $dec_perms = (stat("filename"))[2] & 07777;
                my $oct_perm_str = sprintf "%o", $perms;

            The "oct" function is commonly used when a string such as 644
            needs to be converted into a file mode, for example. Although
            Perl automatically converts strings into numbers as needed, this
            automatic conversion assumes base 10.

            Leading white space is ignored without warning, as too are any
            trailing non-digits, such as a decimal point ("oct" only handles
            non-negative integers, not negative integers or floating point).

Related topics

Other articles