Front page | perl.perl5.porters |
Postings from July 2011
Re: [PATCH] Support for 0o/0O octal strings
Thread Previous
|
Thread Next
From:
Claes Jakobsson
Date:
July 4, 2011 01:16
Subject:
Re: [PATCH] Support for 0o/0O octal strings
Message ID:
F77365F2-AD72-442C-9FD2-38C163DD3C97@surfar.nu
On 4 jul 2011, at 08.19, H.Merijn Brand wrote:
> On Mon, 4 Jul 2011 00:27:22 +0200, Claes Jakobsson <claes@surfar.nu>
> wrote:
>>
>> Interesting. The same behavior (but reversed) exists with 'x'
>>
>> $ ./miniperl -e '0x foo()'
>> Bareword found where operator expected at -e line 1, near "0x foo"
>> (Missing operator before foo?)
>> syntax error at -e line 1, near "0x foo"
>> Execution of -e aborted due to compilation errors.
>>
>> $ ./miniperl -e '0 x foo()'
>> Undefined subroutine &main::foo called at -e line 1.
>
> There is a subtle difference here. I don't know how deep this goes into
> the parser, but in the 'x' case, "x" is a single byte op and need does
> only need to look one byte ahead (in the 0x case). If the next byte is
> one of [0-9a-fA-F], the parser could safely assume hex. If the two are
> not glued, the x can be anything ranging from the "x" operator to the
> first character of a function named x_or_y_or_whatever.
>
> In the case of 'o', there is no single byte operator called "o", and
> historically there has not been 0o followed by [0-7], so parsing would
> be hard to disambiguate.
Is there any other situation where 0o<followed by something> is legal apart from 0or?
>> But yes, it should be possible the retain backwards compatibility if
>> we also that the character following 'o' is valid octal.
>>
>> } else if ((s[1] == 'o' || s[1] == 'O') && (s[2] >= '0' && s[2] <= '7')) {
>>
>> now my
>>
>> $ ./miniperl -e '0or foo()'
>> Undefined subroutine &main::foo called at -e line 1.
>>
>> $ ./miniperl -e 'print 0o31'
>> 25
>
> $ perl -wle'print 0x'
> 0
>
> (without patch)
>
> $ perl -wle'print 0o'
> Bareword found where operator expected at -e line 1, near "0o"
> (Missing operator before o?)
> Unquoted string "o" may clash with future reserved word at -e line 1.
> syntax error at -e line 1, near "0o
> "
> Execution of -e aborted due to compilation errors.
>
>
> Will that also print 0?
Is 0x being legal in perl deliberate or an unforseen sideeffect of how it's implemented. It's illegal in Java and C99.
>> Regarding the benefit of 0o I don't see any good reason either
>> except it was listed in perltodo and it can be abused for poetry =)
>>
>> Do we want this feature really? I'd rather see an arbitrary base
>> numeric literal like erlang has.
>>
>>> 5#41.
>> 21
>
> Not with #, that will break comments. More intuitive would be the
> current syntax error ^^
>
> $ perl -wle'print 5^^16'
> syntax error at -e line 1, near "^^"
> Execution of -e aborted due to compilation errors.
>
>>> 28#Perl.
>> 560553
>>
>> Perhaps a syntax like 0[<base>]<number> (suggestions welcome!),
>> the above would then be 0[28]Perl. Erlang limits the base to 36
>> tho as this is where it runs out of 0-9A-Z.
>
> I wonder how often that would be used in real (production) code, as
> that for sure needs a lot of comment lines. IMHO it is much easier to
> implement three functions in XS in a CPAN module that do base
> conversions
>
> my $base16 = tonumbase (16, 8364); # 20ac
> my $base10 = fromnumbase (16, 8364); # 33636
> my $base8 = fromtonumbase (16, 8, 8364); # 101544
>
> You could even add some optional last argument(s)
>
> my $base16 = tonumbase (16, 8364, "0x"); # 0x20ac
> my $base10 = fromnumbase (16, 8364); # 33636
> my $base8 = fromtonumbase (16, 8, 8364, 1); # 0101544
>
> just brainstorming there (the optional argument - when passed - adds a
> (predefined) prefix unless the content is a unknown prefix in which
> case it prefixes the passed argument.
Why erlang choose to include it in their syntax I don't know since it would be equally easy to do it as a module for them too. Perhaps it's handy when dealing with telecom protocols.
There's a couple of modules on CPAN that already does this, Math::Int2Base and Math::NumberBase so no need for yet another module.
There seems to be a few applications of non-standard bases tho. http://en.wikipedia.org/wiki/Positional_notation#Applications
/Claes
Thread Previous
|
Thread Next