This page was generated at 2008-05-17 02:01:06 GMT.
(syn r14541, pugs-tests r20436, pugs-smoke r19912)
  [ Index of Synopses ]

Title

Synopsis 29: Builtin Functions

Version

 Author:        Rod Adams <rod@rodadams.net>
 Maintainer:    Larry Wall <larry@wall.org>
 Contributions: Aaron Sherman <ajs@ajs.com>
                Mark Stosberg <mark@summersault.com>
 Date:          12 Mar 2005
 Last Modified: 31 Jan 2008
 Version:       20

This document attempts to document the list of builtin functions in Perl 6. It assumes familiarity with Perl 5 and prior synopses.

The document is now the official S29. It's still here in the pugs repository temporarily to allow easy access to pugs implementors, but eventually it will be copied over to svn.perl.org. Despite its being "official", feel free to hack on it as long as it's in the pugs space. -law

This document is generated from the pod in the pugs repository under /docs/Perl6/Spec/Functions.pod so edit it there in the SVN repository if you would like to make changes.

Notes

In Perl 6, all builtin functions belong to a named package (generally a class or role). Not all functions are guaranteed to be imported into the global package ::*. In addition, the list of functions imported into ::* will be subject to change with each release of Perl. Authors wishing to "Future Proof" their code should either specifically import the functions they will be using, or always refer to the functions by their full name.

After 6.0.0 comes out, global aliases will not be removed lightly, and will never be removed at all without having gone through a deprecation cycle of at least a year. In any event, you can specify that you want the interface for a particular version of Perl, and that can be emulated by later versions of Perl to the extent that security updates allow.

Where code is given here, it is intended to define semantics, not to dictate implementation.

Operators vs. Functions

There is no particular difference between an operator and a function, but for the sake of documentation, only functions declared without specifying a grammatical category or with a category of term: (see "Bits and Pieces" in S02) will be described as "functions", and everything else as "operators" which are outside of the scope of this document.

Multis vs. Functions

In actual fact, most of the "functions" defined here are multi subs, or are multi methods that are also exported as multi subs. Multi subs are all visible in the global namespace (unless declared with a "my multi"). The assumption is that with sufficiently specific typing on the multis, the user is free to extend a particular name to new types.

Type Declarations

From t/spec/S29-type/declarations.t lines 4–29 (no results): (skip)

 

The following type declarations are assumed:

AnyChar

The root class of all "character" types, regardless of level.

This is a subtype of Str, limited to a length of 1 at it's highest supported Unicode level.

The type name Char is aliased to the maximum supported Unicode level in the current lexical scope (where "current" is taken to mean the eventual lexical scope for generic code (roles and macros), not the scope in which the generic code is defined). In other words, use Char when you don't care which level you're writing for.

Subclasses (things that are isa AnyChar):

CharLingua (language-defined characters)
Grapheme (language-independent graphemes)
Codepoint
Byte

Yes, Byte is both a string and a number.

The short name for Grapheme is typically Char since that's the default Unicode level. A grapheme is defined as a base codepoint plus any subsequent "combining" codepoints that apply to that base codepoint. Graphemes are always assigned a unique integer id which, in the case of a grapheme that has a precomposed codepoint, happens to be the same as that codepoint.

There is no short name for CharLingua because the type is meaningless outside the scope of a particular language declaration. In fact, CharLingua is itself an abstract type that cannot be instantiated. Instead you have names like CharFrench, CharJapanese, CharTurkish, etc. for instantiated CharLingua types. (Plus the corresponding StrLingua types, presumably.)

Matcher
 subset Matcher of Item | Junction;

Used to supply a test to match against. Assume ~~ will be used against it.

Ordering
 subset KeyExtractor of Code where { .sig === :(Any --> Any) };
 subset Comparator   of Code where { .sig === :(Any, Any --> Int ) };
 subset OrderingPair of Pair where { .left ~~ KeyExtractor && .right ~~ Comparator };
 subset Ordering where Signature | KeyExtractor | Comparator | OrderingPair;

Used to handle comparisons between things. Generally this ends up in functions like cmp(), eqv(), sort(), min(), max(), etc., as a $by parameter which provides the information on how two things compare relative to each other.

Note that eqv() and cmp() do almost but not the same thing since with eqv() you don't care if two things are ordered increasing or decreasing but only if they are the same or not. Rather than declare an Equiving type declaration Ordering will just do double duty.

Comparator

A closure with arity of 2, which for ordering returns negative/zero/positive, signaling the first argument should be before/tied with/after the second. aka "The Perl 5 way".

For equivalence the closure returns either not 0 or 0 indicating if the first argument is equivalent or not to the second.

KeyExtractor

A closure with arity of 1, which returns the "key" by which to compare. Values are compared using cmp for orderings and eqv for equivalences, which in Perl 6 do different comparisons depending on the types. (To get a Perl 5 string ordering you must compare with leg instead.)

Internally the result of the KeyExtractor on a value should be cached.

OrderingPair

A combination of the two methods above, for when one wishes to take advantage of the internal caching of keys that is expected to happen, but wishes to compare them with something other than eqv or cmp, such as <=> or leg.

Signature

If a signature is specified as a criterion, the signature is bound to each value and then each parameter does comparisons in positional order according to its type, as modified by its traits. Basically, the system will write the body of the key extraction and comparison subroutine for you based on the signature.

For ordering the list of positional parameter comparisons is reduced as if using [||] but all comparisons do not need to be performed if an early one determines an increasing or decreasing order. For equivalence the list is reduced as if using [&&].

Function Packages

Any

The following are defined in the Any role:

eqv

From t/spec/S29-any/eqv.t lines 6–119 (no results): (skip)

 
 our Bool multi sub eqv (Ordering @by, $a, $b)
 our Bool multi sub eqv (Ordering $by = &infix:<eqv>, $a, $b)

Returns a Bool indicating if the parameters are equivalent, using criteria $by or @by for comparisons. @by differs from $by in that each criterion is applied, in order, until a non-zero (equivalent) result is achieved.

cmp

From t/spec/S29-any/cmp.t lines 6–11 (3 √, 0 ×): (skip)

 
 our Order multi sub cmp (Ordering @by, $a, $b)
 our Order multi sub cmp (Ordering $by = &infix:<cmp>, $a, $b)

Returns Order::Increase, or Order::Same, or Order::Decrease (which numify to -1, 0, +1 respectively) indicating if paramater $a should be ordered before/tied with/after parameter $b, using criteria $by or @by for comparisons. @by differs from $by in that each criterion is applied, in order, until a non-zero (tie) result is achieved. If the values are not comparable, returns a proto Order object that is undefined.

Num

The following are all defined in the Num role:

API document: Num

Num provides a number of constants in addition to the basic mathematical functions. To get these constants, you must request them:

From t/spec/S29-num/pi.t lines 5–36 (0 √, 6 ×): (skip)

 
 use Num :constants;

or use the full name, e.g. Num::pi.

abs

From t/spec/S29-num/complex.t lines 36–49 (no results): (skip)

 

From t/spec/S29-num/abs.t lines 5–35 (8 √, 0 ×): (skip)

 
 our Num multi method abs ( Num $x: ) is export

Absolute Value.

floor

From t/spec/S29-num/rounders.t lines 6–49 (no results): (skip)

 
 our Int multi method floor ( Num $x: ) is export

Returns the highest integer not greater than $x.

ceiling

From t/spec/S29-num/rounders.t lines 8–49 (no results): (skip)

 
 our Int multi method ceil ( Num $x: ) is export

Returns the lowest integer not less than $x.

round

From t/spec/S29-num/rounders.t lines 5–49 (no results): (skip)

 
 our Int multi method round ( Num $x: ) is export

Returns the nearest integer to $x. The algorithm is floor($x + 0.5). (Other rounding algorithms will be given extended names beginning with "round".)

truncate

From t/spec/S29-num/int.t lines 5–81 (no results): (skip)

 

From t/spec/S29-num/rounders.t lines 7–49 (no results): (skip)

 
 our Int multi method truncate ( Num $x: ) is export
 our Int multi method int ( Num $x: ) is export

Returns the closest integer to $x whose absolute value is not greater than the absolute value of $x. (In other words, just chuck any fractional part.) This is the default rounding function used by an int() cast, for historic reasons. But see Int constructor above for a rounded version.

exp

From t/spec/S29-num/exp.t lines 5–30 (0 √, 6 ×): (skip)

 
 our Num multi method exp ( Num $exponent: Num :$base = Num::e ) is export

Performs similar to $base ** $exponent. $base defaults to the constant e.

log

From t/spec/S29-num/log.t lines 11–15 (no results): (skip)

 
 our Num multi method log ( Num $x: Num :$base = Num::e ) is export

Logarithm of base $base, default Natural. Calling with $x == 0 is an error.

log10

From t/spec/S29-num/log.t lines 16–45 (no results): (skip)

 
 our Num multi method log10 (Num $x:) is export

A base 10 logarithm, othewise identical to log.

rand

From t/spec/S29-num/rand.t lines 13–22 (4 √, 0 ×): (skip)

 
 our Num method rand ( Num $x: )
 our Num term:<rand>

Pseudo random number in range 0 ..^ $x. That is, 0 is theoretically possible, while $x is not. The rand function is 0-ary and always produces a number from 0..^1. In any case, for picking a random integer you probably want to use something like (1..6).pick instead.

sign

From t/spec/S29-num/sign.t lines 5–19 (6 √, 0 ×): (skip)

 
 our Int multi method sign ( Num $x: ) is export

Returns 1 when $x is greater than 0, -1 when it is less than 0, 0 when it is equal to 0, or undefined when the value passed is undefined.

srand

From t/spec/S29-num/rand.t lines 23–44 (4 √, 0 ×): (skip)

 
 multi method srand ( Num $seed: )
 multi srand ( Num $seed = default_seed_algorithm())

Seed the generator rand uses. $seed defaults to some combination of various platform dependent characteristics to yield a non-deterministic seed. Note that you get one srand() for free when you start a Perl program, so you must call srand() yourself if you wish to specify a deterministic seed (or if you wish to be differently nondeterministic).

sqrt

From t/spec/S29-num/sqrt.t lines 5–31 (6 √, 1 ×): (skip)

 
 our Num multi method sqrt ( Num $x: ) is export

Returns the square root of the parameter.

roots

From t/spec/S29-num/roots.t lines 5–40 (no results): (skip)

 
  (in Num) method roots (Num $x: Int $n --> List of Num) is export

Returns a list of all $nth (complex) roots of $x

cis

From t/spec/S29-num/complex.t lines 11–24 (no results): (skip)

 

From t/spec/S29-num/complex.t lines 25–34 (no results): (skip)

 
    our Complex multi method cis (Num $angle:) is export

Returns 1.unpolar($angle)

unpolar

From t/spec/S29-num/complex.t lines 26–34 (no results): (skip)

 

From t/spec/S29-num/complex.t lines 35–49 (no results): (skip)

 

From t/spec/S29-num/complex.t lines 50–60 (no results): (skip)

 
    our Complex multi method unpolar (Num $mag: Num $angle) is export

Returns a complex number specified in polar coordinates. Angle is in radians.

Complex

    our Seq multi method polar (Complex: $nim) is export

Returns (magnitude, angle) corresponding to the complex number. The magnitude is non-negative, and the angle in the range -π ..^ π.

The :Trig tag

From t/spec/S29-trig/trig.t lines 5–96 (no results): (skip)

 

From t/spec/S29-trig/e.t lines 5–21 (no results): (skip)

 

The following are also defined in Num but not exported without a :Trig tag. (Which installs their names into Num::Trig, as it happens.)

Standard Trig Functions
 Num multi method func ( Num  $x: $base = 'radians' ) is export(:Trig)

where func is one of: sin, cos, tan, asin, acos, atan, sec, cosec, cotan, asec, acosec, acotan, sinh, cosh, tanh, asinh, acosh, atanh, sech, cosech, cotanh, asech, acosech, acotanh.

Performs the various trigonometric functions.

Option $base is used to declare how you measure your angles. Given the value of an arc representing a single full revolution.

 $base      Result
 ----       -------
 /:i ^r/    Radians  (2*pi)
 /:i ^d/    Degrees  (360)
 /:i ^g/    Gradians (400)
 Num        Units of 1 revolution.

Note that module currying can be used within a lexical scope to specify a consistent base so you don't have to supply it with every call:

 my module Trig ::= Num::Trig.assuming(:base<degrees>);

This overrides the default of "radians".

atan2
 our Num multi method atan2 ( Num $y: Num $y = 1 )
 our Num multi atan2 ( Num $y, Num $x = 1 )

This second form of atan computes the arctangent of $y/$x, and takes the quadrant into account. Otherwise behaves as other trigonometric functions.

Scalar

API document: Scalar

Scalar provides the basic tools for operating on undifferentiated scalar variables. All of the following are exported by default.

defined

From t/spec/S29-scalar/defined.t lines 5–72 (21 √, 0 ×): (skip)

 
  our Bool multi defined ( Any $thing )
  our Bool multi defined ( Any $thing, ::role )

defined returns true if the parameter has a value and that value is not the undefined value (per undef), otherwise false is returned.

Same as Perl 5, only takes extra optional argument to ask if value is defined with respect to a particular role:

  defined($x, SomeRole);

A value may be defined according to one role and undefined according to another. Without the extra argument, defaults to the definition of defined supplied by the type of the object.

undefine

From t/spec/S29-scalar/undef.t lines 57–203 (no results): (skip)