Hi,
Many thanks for all the feedback about the literals document.
This new version integrates most of the changes. I've also added a
subsection about Inf and NaN, directly coming from Michael's
perlval.
I've also changed the pod syntax to =section, as suggested. I've used
the:
=section **
syntax to specify level, because this is the one I prefer, but I'll
change this once we agree in the final format.
Comments welcome,
-angel
------------------------------------------------------------------------
=section Literal Values
=section * Literal numbers
=section ** Integers
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
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:
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
Finally, you can create negative integers by prepending the C<->
character.
For example:
my $x = 18;
my $y = -18;
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;
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
=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>.