develooper Front page | perl.perl5.porters | Postings from July 2011

Re: [PATCH] Support for 0o/0O octal strings

Thread Previous | Thread Next
From:
H.Merijn Brand
Date:
July 3, 2011 23:20
Subject:
Re: [PATCH] Support for 0o/0O octal strings
Message ID:
20110704081957.4ef69c91@pc09.procura.nl
On Mon, 4 Jul 2011 00:27:22 +0200, Claes Jakobsson <claes@surfar.nu>
wrote:

> 
> On 3 jul 2011, at 23.43, Father Chrysostomos wrote:
> > Claes Jakobsson wrote:
> >> Hi,
> >> 
> >> the attached patches
> >> 
> >> 1) Adds support + tests for 0o and 0O octal string constants
> >> 2) Adds 0o and 0O to grok_oct hence making oct() also understand the above + tests
> >> 3) removes the task from perltodo
> >> 
> >> /Claes
> > 
> > Before the patches:
> > 
> > $ ./miniperl -e '0or foo()'
> > Undefined subroutine &main::foo called at -e line 1.
> > 
> > After the patches:
> > 
> > $ ./miniperl -e '0or foo()'
> > Bareword found where operator expected at -e line 1, near "0or"
> > 	(Missing operator before r?)
> > syntax error at -e line 1, near "0or foo"
> > Execution of -e aborted due to compilation errors.
> > 
> > Is there any way to resolve this in favour of backward compatibility? (Honestly, I don’t see what benefit this feature provides, even though it’s listed in perltodo.)
> 
> 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.

> 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?

> 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.

-- 
H.Merijn Brand  http://tux.nl      Perl Monger  http://amsterdam.pm.org/
using 5.00307 through 5.14 and porting perl5.15.x on HP-UX 10.20, 11.00,
11.11, 11.23 and 11.31, OpenSuSE 10.1, 11.0 .. 11.4 and AIX 5.2 and 5.3.
http://mirrors.develooper.com/hpux/           http://www.test-smoke.org/
http://qa.perl.org      http://www.goldmark.org/jeff/stupid-disclaimers/

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About