Re: Literals, take 2
[email protected] ("Erik Steven Harrison") Mon, 18 Nov 2002 07:08:32 -0800
| Newsgroups | perl.perl6.documentation |
|---|---|
| Organization | Angelfire (http://email.angelfire.mailcity.lycos.com:80) |
| Message-ID | <[email protected]> |
--
On 17 Nov 2002 11:09:53 -050
Bryan C. Warnock wrote:
>On Wed, 2002-11-13 at 13:26, Angel Faus wrote:
>>
>> There are many ways to specify literal numeric values in perl, but
>> they default to base 10 for input and output. Once the number has
>
>Surely, Perl 6 will allow changing the radix on a more global scale.
Of course it will! But just about anything acn be changed with a grammer munge. Theoretically Perl 5 could add radix notation with a source filter. But we don't have to document that.
I think that we should avoid refering to pragmas and modules as much as possible in the core documentation. That's a departure from Perl 5. Here's my case:
* Were writing the spec. While that doesn't give us
licence to be dense and unreadable, we should leave
common case information (like documenting the radix
pragma) for the tutorials.
* Perl 6 will likely not have a standard set of
modules and pragmas. As that is the case, we can't
assume modules and pragmas in the docs.
* Perl 6 can do just about any wild and crazy thing with a grammer munge. Instead of documenting pragmas like radix piecemeal, we should be beating the concept that a use declaration means all bets are off into everybody's heads (beating in a pleasant metaphorical sense. of course). When a use is seen that modules documentation should be grabbed, not the core docs.
-Erik
>
>use radix(16); # or something of the ilk
>
>in which case, the default (ie, non-prefixed) radix will be some
>property, which itself defaults to base 10.
>
>In any case, you should present the radix prefix semantics globally,
>and then indicate when and where you can imply the radix.
>
>That will alleviate confusion on some of the fringe cases - or perhaps
>the confusion is only mine. :-)
>
>
>> been read by perl it becomes just a magnitude. That is it loses all
>> trace of the way it was originally represented and is just a number.
>> This code for instance prints the literal value 14.
>>
>> my $x = 14; # stores the integer 14 in $x
>> print $x;
>>
>> You can represent the literal value in any other base, using the
>> C<radix:dddddddd> syntax.
>>
>> For example:
>>
>> my $i = 2:101110; # binary
>> my $j = 3:1210112; # tertiary
>> my $k = 8:1270; # octal
>>
>> Printing these would give 46, 1310, and 696 respectively. When the
>> base is greater than 10, there is a need to represent digits that are
>> greater than 9.
>>
>> You can do this in two ways:
>>
>> =over
>>
>> =item *
>>
>> Alphabetic characters: Following the standard convention, perl will
>> interpret the A letter as the digit 10, the B letter as digit 11, and
>> so on.
>>
>> my $l = 16:1E3A7; # hexadecimal
>>
>> =item *
>>
>> Separating by dots: You can also write each digit in its decimal
>> representation, and separate digits using the C<.> character.
>>
>> my $m = 256:255.255.255.0; # 256-base
>>
>> =back
>>
>> For example, the integer 30 can be written in hexadecimal base in two
>> equivalent ways:
>
>Do these ways have names?
>
>>
>> my $x = 16:1D
>> my $x = 16:1.14
>>
>> These two representations are incompatible, so writing something like
>> C<16:D.13> will generate a compile-time error.
>>
>> Also note that a compile-time error will be generated if you specify a
>> "digit" that is larger than your radix can support. For instance,
>>
>> my $x = 3:23; # error
>
>How does one specify a single digit in the... that second one there.
>The extended notation.
>
>my $x = 16:12; # 10:18
>my $x = 16:12.; # 10:12 ?
>
>Also, given an implied radix, can one use the second extended notation?
>
>my $x = 4.5; # Same as 45 in base 10
>
>use radix(16);
>my $x = 14.8.10; # Same as 0x0d8a;
>
>If so, what about the two digit ambiguity?
>
>>
>> Finally, you can create negative integers by prepending the C<->
>> character.
>
>To what? To the digits? To the radix?
>
>>
>> For example:
>>
>> my $x = 18;
>> my $y = -18;
>
>my $x = 10:18;
>my $y = -10:18;
>my $z = 10:-18;
>
>On one hand, you've negated a base 10 number. OTOH, you've got a base
>10 negative number. I can think in both directions, but I'm afraid
>some folks may be confused with the first, whereas the second is clear
>and just as messy.
>
>
>>
>> Perl allows the underline character, C<_>, to be placed as a separator
>> between the digits of any literal number. You can use this to break
>> up long numbers into more readable forms. There aren't any rules to
>> it; you can use it however you like:
>>
>> 123_456_000.000 (floating point)
>> 2:0110_1000 (binary)
>> 16:FF_88_EE (hexidecimal)
>>
>> =section ** Floating-Point Numbers
>>
>> You can use the decimal representation of the number and the standard
>> exponental notation.
>>
>> my $x = -2.542;
>> my $x = 7.823e12;
>
>Not wanting implementation to drive the language, but the parsing of
>integer and numerical values may all be built upon above. Going back
>to above, is radix parsing core to numerical parsing? (IOW, is a radix
>prefix possible *everywhere* an integer is wanted, or is radix notation
>its own beastie which then wants an integer type? [Much of the same
>argument as above with negative numbers.])
>
>my $x = 7.832e16:D; # error?
>
>>
>> Perl lets you operate integer numbers with floating-point numbers, so
>> the following operation is correct:
>>
>> print 3.5 + 2; # prints 5.5
>>
>> You can use the C<_> separator too in Floating-Point numbers:
>>
>> my $x = 1312512.25; # floating-poing number
>> my $x = 1_312_512.25; # the same
>
>1_234.345_123
>
>1_345.678_123e1_345;
>
>>
>> =section ** Pseudo-numbers
>>
>> The terms C<+Inf> and C<-Inf> represent positive and negative
>> infinity; you may sometimes use these to create infinite lists.
>>
>> The value C<NaN> ("Not a Number") may be returned by some functions
>> or operations to represent that the result of a calculation (for
>> example, division by zero) cannot be represented by a numeric value.
>>
>> =section * Literal strings
>>
>> Duble quotes or single quotes may be used around literal strings:
>>
>> print "Hello, world";
>> print 'Hello, world';
>>
>> However, only double quotes "interpolate" variables and special
>> characters such as newlines ("\n"):
>>
>> print "Hello, $name\n"; # works fine
>> print 'Hello, $name\n'; # prints $name\n literally
>>
>> my $x = 'world';
>> print "Hello, $x"; # Hello, world
>>
>> You can type any character including newlines and tabs in literal
>> strings:
>>
>> my $x = 'Look mum, I can type
>> newlines and tabs too!';
>>
>> If you use a Unicode editor to edit your program, Unicode characters
>> may occur directly within the literal strings.
>>
>> Unicode characters can also be added to a double-quoted string using
>> the C<\x{...}> notation. The Unicode code for the desired character,
>> in hexadecimal, should be placed in the braces. For instance, a
>> smiley face is C<\x{263A}>.
>>
>> Perl provides escapes for easily inserting characters that have a
>> codepoint below 256:
>>
>> print "\xB" # Hexadecimal - character number 16:xB
>> print "\024" # Octal - character number 8:33
>>
>> Aditionally, you can use the C<\N{...}> notation and put the official
>> Unicode character name within the braces, such as C<\N{WHITE SMILING
>> FACE}>.
>>
>> See the L<quotes> section for a full explanation of the interpolation
>> mechanism and a list of special characters in double-quoted strings.
>>
>> =section ** String as vector of ordinals
>>
>> Literals of the form C<v1.2.3.4> are parsed as a string composed of
>> characters with the specified ordinals. This is an alternative, more
>> readable way to construct (possibly unicode) strings instead of
>> interpolating characters, as in C<\x{1}\x{2}\x{3}\x{4}>. The leading
>> C<v> may be omitted if there are more than two ordinals, so C<1.2.3>
>> is parsed the same as C<v1.2.3>.
>
>See numerical comment above about the implied radix.
>
>
>--
>Bryan C. Warnock
>bwarnock@(gtemail.net|raba.com)
>
____________________________________________________________
Get 25MB of email storage with Lycos Mail Plus!
Sign up today -- http://www.mail.lycos.com/brandPage.shtml?pageId=plus