Skip to content

Latest commit

 

History

History
428 lines (241 loc) · 6.71 KB

builtins.pod

File metadata and controls

428 lines (241 loc) · 6.71 KB

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.

Numbers

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 as 12, it is an Int.

Num

Num is the floating point type. It stores sign, mantissa, and exponent, each with a fixed width. Calculations involving Num numbers are usually quite fast, though subject to limited precision.

Numbers in scientific notation such as 6.022e23 are of type Num.

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 on Rats with large components can become quite slow. For this reason, rationals with large denominators automatically degrade to Num.

Writing a fractional value with a dot as the decimal separator, such as 3.14, produces a Rat.

Complex

Complex numbers have two parts: a real part and an imaginary part. If either part is NaN, then the entire number may possibly be NaN.

What is i, if not sqrt(-1)?

Numbers in the form a + bi, where bi is the imaginary component, are of type Complex.

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

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

Bool

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 to False. All other strings evaluate to True.

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

POD ERRORS

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])