Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting to and from octal in Perl 6

Tags:

raku

If we have an octal number, e.g. 0o157, Perl 6 can convert it into decimal:

> 0o157
111

We are not permitted to remove this o in its octal representation:

> 0157
Potential difficulties:
    Leading 0 has no meaning. If you meant to create an octal number, use '0o' prefix; like, '0o157'. If you meant to create a string, please add quotation marks.
    ------> 0157⏏<EOL>

Now let's make a reverse conversion, from decimal to octal:

> printf "%#o\n", 111
0157

The question is: why is there now no o after 0 in the octal representation?

Meanwhile, if we convert to hexadecimal, the x will be there:

> printf "%#x\n", 111
0x6f
like image 850
Eugene Barsky Avatar asked Jan 21 '18 21:01

Eugene Barsky


1 Answers

The question is: why is there now no o after 0 in the octal representation?

(s)printf is a pretty universally used function, and ported directly to Perl 6. It is meant to be compatible with other languages' printf functions more than with Perl 6 input syntax.

Perl 5's printf behaves the same way, so its behavior was likely copied directly.

like image 162
moritz Avatar answered Sep 22 '22 06:09

moritz