I'm not sure if this qualifies as a chapter on its own. I could imagine putting this at the end of the first chapter, or squeeze the sections into chapter 1 where appropriate.
Many operators work on a particular type of data. If the type of the operands differs from the type of the operand, Perl will make copies of the operands and convert them to the needed types. For example, $a + $b
will convert a copy of both $a
and $b
to numbers (unless they are numbers already). This implicit conversion is called coercion.
Besides operators, other syntactic elements coerce their elements: if
and while
coerce to truth values (Bool
), for
views things as lists, and so on.
I've explained this as a form of context in the Modern Perl book; it may work here too.
Sometimes coercion is transparent. Perl 6 has several numeric types which can intermix freely--such as subtracting a floating point value from an integer, as 123 - 12.1e1
.
The most important types are:
- Int
-
Int
objects store integer numbers of arbitrary size. If you write a literal that consists only of digits, such as12
, it is anInt
. - Num
-
Num
is the floating point type. It stores sign, mantissa, and exponent, each with a fixed width. Calculations involvingNum
numbers are usually quite fast, though subject to limited precision.Numbers in scientific notation such as
6.022e23
are of typeNum
. - Rat
-
Rat
, short for rational, stores fractional numbers without loss of precision. It does so by tracking its numerator and denominator as integers, so mathematical operations onRat
s with large components can become quite slow. For this reason, rationals with large denominators automatically degrade toNum
.Writing a fractional value with a dot as the decimal separator, such as
3.14
, produces aRat
. - Complex
-
Complex
numbers have two parts: a real part and an imaginary part. If either part isNaN
, then the entire number may possibly beNaN
.What is
i
, if not sqrt(-1)?Numbers in the form
a + bi
, wherebi
is the imaginary component, are of typeComplex
.
The following operators are available for all number types:
Most mathematical functions are available both as methods and functions, so you can write both (-5).abs
and abs(-5)
.
Gradians? Are these functions also methods?
The trigonometric functions sin
, cos
, tan
, asin
, acos
, atan
, sec
, cosec
, cotan
, asec
, acosec
, acotan
, sinh
, cosh
, tanh
, asinh
, acosh
, atanh
, sech
, cosech
, cotanh
, asech
, acosech
and acotanh
work in units of radians by default. You may specify the unit with an argument of Degrees
, Gradians
or Circles
. For example, 180.sin(Degrees)
is approximately 0
.
Strings stored as Str
are sequences of characters, independent of character encoding. The Buf
type is available for storing binary data. The encode
method converts a Str
to Buf
. decode
goes the other direction.
The following operations are available for strings:
TODO: Str and Buf operators, methods
A Boolean value is either True
or False
. Any value can coerce to a boolean in boolean context. The rules for deciding if a value is true or false depend on the type of the value:
- Strings
-
Empty strings and
"0"
evaluate toFalse
. All other strings evaluate toTrue
. - Numbers
-
All numbers except zero evaluate to
True
. - Lists and Hashes
-
Container types such as lists and hashes evaluate to
False
if they are empty, and toTrue
if they contain at least one value.
Constructs such as if
automatically evaluate their conditions in boolean context. You can force an explicit boolean context by putting a ?
in front of an expression. The !
prefix negates the boolean value.
Hey! The above document had some coding errors, which are explained below:
- Around line 1:
-
Unknown directive: =head0
- Around line 9:
-
=end for without matching =begin. (Stack: [empty])
- Around line 29:
-
=end for without matching =begin. (Stack: [empty])
- Around line 88:
-
=end for without matching =begin. (Stack: =over)
- Around line 246:
-
=end for without matching =begin. (Stack: [empty])
- Around line 381:
-
=end for without matching =begin. (Stack: [empty])