Specify octal numbers with the 0o prefix

Perl v5.34 allows you to specify octal literals with the 0o prefix, as in 0o123_456. This is consistent with the existing constructs to specify hexadecimal literal 0xddddd and binary literal 0bddddd. The builtin oct() function accepts any of these forms.

Previously, you specified octal with just a leading zero:

chmod 0644, $file;
mkdir 0755, $file;

Now you can do that an extra character that specifies the base:

chmod 0o644, $file;
mkdir 0o755, $file;

This makes it consistent with 0b for binary and 0x for hexadecimal. See “Scalar value constructors” in perldata.

And, remember that v5.14 added the \o{NNN} notation to specify characters by their octal number. We’re still waiting for octal floating point values (we got the hex version in v5.22), but don’t hold your breath.

Perhaps we’ll get 0d sometime so that all the bases.

Leave a comment

2 Comments.

  1. Peter John Acklam

    Octal floating point numbers seem to be implemented:

    # Perl 5.33.8 with quadmath using “0o” prefix:

    $ perl -le ‘print 0o1.4441766521041321410646114314242700334p+1’
    3.1415926535897932384626433832795

    # Perl 5.33.8 without quadmath using “0o” prefix:

    $ perl -le ‘print 0o1.4441766521041321p+1’
    3.14159265358979

    # Perl 5.33.8 with quadmath using “0” prefix:

    $ perl -le ‘print 01.4441766521041321410646114314242700334p+1’
    3.1415926535897932384626433832795

    # Perl 5.33.8 without quadmath using “0” prefix:

    $ perl -le ‘print 01.4441766521041321p+1’
    3.14159265358979

    # Math::BigFloat->from_oct() for reference:

    $ perl -MMath::BigFloat -le ‘print Math::BigFloat->from_oct(“1.4441766521041321410646114314242700334p+1”)’
    3.1415926535897932384626433832795…

Leave a Reply


[ Ctrl + Enter ]