Documentation for routine new

Documentation for routine new, assembled from the following types:

class Mu

From Mu

(Mu) method new

multi method new(*%attrinit)

Default method for constructing (create + initialize) new objects of a class. This method expects only named arguments which are then used to initialize attributes with accessors of the same name.

Classes may provide their own new method to override this default.

new triggers an object construction mechanism that calls submethods named BUILD in each class of an inheritance hierarchy, if they exist. See the documentation on object construction for more information.

class Backtrace

From Backtrace

(Backtrace) method new

Defined as:

multi method new(--> Backtrace:D)

Creates a new backtrace, using its calling location as the origin of the backtrace.

my $backtrace =;

role Blob

From Blob

(Blob) method new

Defined as:

method new(*@codes)

Creates a Blob from a list of integers.

my $blob =[123]);

class Complex

From Complex

(Complex) method new

Defined as:

multi method new(Real $reReal $im --> Complex:D)

Creates a new Complex object from real and imaginary parts.

my $complex =;

class ComplexStr

From ComplexStr

(ComplexStr) method new

method new(Complex $iStr $s)

The constructor requires both the Complex and the Str value, when constructing one directly the values can be whatever is required:

my $f =, "forty two (but complicated)");
say +$f# OUTPUT: «42+0i␤» 
say ~$f# OUTPUT: «"forty two (but complicated)"␤» 

class Date

From Date

(Date) method new

Defined as:

multi method new($year$month$day:&formatter --> Date:D)
multi method new(:$year!:$month = 1:$day = 1  --> Date:D)
multi method new(Str $date                        --> Date:D)
multi method new(Instant:D $dt                    --> Date:D)
multi method new(DateTime:D $dt                   --> Date:D)

Creates a new Date object, either from a triple of (year, month, day) that can be coerced to integers, or from a string of the form YYYY-MM-DD (ISO 8601), or from an Instant or DateTime object. Optionally accepts a formatter as a named parameter.

my $date =;
$date = => 2042month => 1day => 1);
$date ="2042-01-01");
$date = 1482155532);
$date =;

class DateTime

From DateTime

(DateTime) method new

Defined as:

multi method new(Int :$year!Int :$month = 1Int :$day = 1,
                 Int :$hour = 0Int :$minute = 0:$second = 0,
                 Int :$timezone = 0:&formatter)
multi method new(Date :$date!,
                 Int :$hour = 0Int :$minute = 0:$second = 0,
                 Int :$timezone = 0:&formatter)
multi method new(Int() $yearInt() $monthInt() $day,
                 Int() $hourInt $minute$second,
                 Int() :$timezone = 0:&formatter)
multi method new(Instant:D $i,  :$timezone=0:&formatter)
multi method new(Int:D $posix,  :$timezone=0:&formatter)
multi method new(Str:D $format:$timezone=0:&formatter)

Creates a new DateTime object. One option for creating a new DateTime object is from the components (year, month, day, hour, ...) separately. Another is to pass a Date object for the date component, and specify the time component-wise. Yet another is to obtain the time from an Instant, and only supply the time zone and formatter. Or instead of an Instant you can supply an Int as a UNIX timestamp.

You can also supply a Str formatted in ISO 8601 timestamp notation or as a full RFC 3339 date and time. Strings should be formatted as yyyy-mm-ddThh:mm:ssZ or yyyy-mm-ddThh:mm:ss+0100. We are somewhat less restrictive than the ISO 8601 standard, as we allow Unicode digits and mixing of condensed and extended time formats.

An invalid input string throws an exception of type X::Temporal::InvalidFormat. If you supply a string that includes a time zone and supply the timezone named argument, an exception of type X::DateTime::TimezoneClash is thrown.

my $datetime = => 2015,
                            month => 1,
                            day => 1,
                            hour => 1,
                            minute => 1,
                            second => 1,
                            timezone => 1);
$datetime = =>'2015-12-24'),
                         hour => 1,
                         minute => 1,
                         second => 1,
                         timezone => 1);
$datetime = First January of 2015 
                         111);   # Hour, minute, second with default timezone 
$datetime =;                       # Instant. 
# from a Unix timestamp 
say $datetime =;            # OUTPUT: «2016-08-10T18:26:23Z␤» 
$datetime ="2015-01-01T03:17:30+0500"# Formatted string 

role Rational

From Rational

(Rational) method new

method new(NuT:D: $numeratorDeT:D: $denominator --> Rational:D)

Creates a new rational object from numerator and denominator.

class Map

From Map

(Map) method new

Defined as:

method new(*@args)

Creates a new Map from a list of alternating keys and values, with the same semantics as described for hash assigning in the Hash documentation, except, for literal pair handling. To ensure pairs correctly get passed, add extra parentheses around all the arguments.

my %h ='a'1'b'2);
# WRONG: :b(2) interpreted as named argument 
say'a'1:b(2) ).keys# OUTPUT: «(a)␤» 
# RIGHT: :b(2) interpreted as part of Map's contents 
say ('a'1:b(2)) ).keys# OUTPUT: «(a b)␤» 

class IO::Path

From IO::Path

(IO::Path) method new

multi method new(Str:D $path)
multi method new(:$basename!:$dirname = '.':$volume = '')

Creates a new IO::Path object from a path string (which is being parsed for volume, directory name and basename), or from volume, directory name and basename passed as named arguments.

class IO::Socket::INET

From IO::Socket::INET

(IO::Socket::INET) method new

multi method new(
        :$family = 2,
        :$encoding = 'utf-8',
        :$nl-in = "\r\n",
    --> IO::Socket::INET:D)
multi method new(
        :$family = 2,
        :$encoding = 'utf-8',
        :$nl-in = "\r\n",
    --> IO::Socket::INET:D)

Creates a new socket.

If :$listen is True, creates a new socket that listen on $localhost (which can be an IP address or a domain name) on port $localport. Otherwise it opens a connection to $host on port $port.

$family defaults to 2 for IPv4, and can be set to 3 for IPv6.

For text operations (such as method lines and method get), $encoding specifies the encoding, and $nl-in determines the character(s) that separate lines.

role IO::Special

From IO::Special

(IO::Special) method new

method new(:$!what!)

The constructor has a single required named argument what that will be '<STDIN>', '<STDOUT>' or '<STDERR>' as appropriate. It is unlikely that you will ever need to construct one of these objects yourself however as it will be done for you when the special handles are initialised.

class IntStr

From IntStr

(IntStr) method new

method new(Int $iStr $s)

The constructor requires both the Int and the Str value, when constructing one directly the values can be whatever is required:

my $f ="forty two");
say +$f# OUTPUT: «42␤» 
say ~$f# OUTPUT: «"forty two"␤» 

class Uni

From Uni

(Uni) method new

method new(*@codes --> Uni:D)

Creates a new Uni instance from the given codepoint numbers.

class Nil

From Nil

(Nil) method new

method new(*@)

Returns Nil

class NumStr

From NumStr

(NumStr) method new

method new(Num $iStr $s)

The constructor requires both the Num and the Str value, when constructing one directly the values can be whatever is required:

my $f ="forty two and a bit");
say +$f# OUTPUT: «42.1␤» 
say ~$f# OUTPUT: «"forty two and a bit"␤» 

class Proc

From Proc

(Proc) method new

method new(Proc:U:
        :$in = '-',
        :$out = '-',
        :$err = '-',
        Bool :$bin = False,
        Bool :$chomp = True,
        Bool :$merge = False,
        Str:D :$enc = 'UTF-8',
        Str:D :$nl = "\n",
    --> Proc:D)
sub run(
        *@args ($*@),
        :$in = '-',
        :$out = '-',
        :$err = '-',
        Bool :$bin = False,
        Bool :$chomp = True,
        Bool :$merge = False,
        Str:D :$enc = 'UTF-8',
        Str:D :$nl = "\n",
        :$cwd = $*CWD,
        Hash() :$env = %*ENV
    --> Proc:D)
sub shell(
        :$in = '-',
        :$out = '-',
        :$err = '-',
        Bool :$bin = False,
        Bool :$chomp = True,
        Bool :$merge = False,
        Str:D :$enc = 'UTF-8',
        Str:D :$nl = "\n",
        :$cwd = $*CWD,
        Hash() :$env = %*ENV
    --> Proc:D)

new creates a new Proc object, whereas run or shell create one and spawn it with the command and arguments provided in @args or $cmd, respectively.

$in, $out and $err are the three standard streams of the to-be-launched program, and default to "-", which means they inherit the stream from the parent process. Setting one (or more) of them to True makes the stream available as an IO::Pipe object of the same name, so for example $proc.out. You can set them to False to discard them. Or you can pass an existing IO::Handle object (for example IO::Pipe) in, in which case this handle is used for the stream.

$bin controls whether the streams are handled as binary (i.e. Blob object) or text (i.e. Str objects). If $bin is False, $enc holds the character encoding to encode strings sent to the input stream and decode binary data from the output and error streams.

With $chomp set to True, newlines are stripped from the output and err streams when reading with lines or get. $nl controls what your idea of a newline is.

If $merge is set to True, the standard output and error stream end up merged in $proc.out.

class Proc::Async

From Proc::Async

(Proc::Async) method new

method new(:$path*@args:$w:$r --> Proc::Async:D)

Creates a new Proc::Async object with external program name or path $path and the command line arguments @args.

If :w is passed to new, then a pipe to the external program's standard input stream (stdin) is opened, to which you can write with write and say. If :r is present then you can tap the processes stdout and stderr.

class Proxy

From Proxy

(Proxy) method new

method new(:&FETCH!:&STORE! --> Proxy:D)

Creates a new Proxy object. &FETCH is called with one argument (the proxy object) when the value is accessed, and must return the value that the fetch produces. &STORE is called with two arguments (the proxy object, and the new value) when a new value is stored in the container.

class RatStr

From RatStr

(RatStr) method new

method new(Rat $iStr $s)

The constructor requires both the Rat and the Str value, when constructing one directly the values can be whatever is required:

my $f ="forty two and a bit");
say +$f# OUTPUT: «42.1␤» 
say ~$f# OUTPUT: «"forty two and a bit"␤» 

class Semaphore

From Semaphore

(Semaphore) method new

method newint $permits )

Initialize the semaphore with the number of permitted accesses. E.g. when set to 2, program threads can pass the acquire method twice until it blocks on the third time acquire is called.

class Seq

From Seq

(Seq) method new

method new(Iterator:D $iter --> Seq:D)

Creates a new Seq object from the iterator passed as the single argument.

class Supplier

From Supplier

(Supplier) method new

method new()

The Supplier constructor.

class Thread

From Thread

(Thread) method new

method new(:&code!Bool :$app_lifetime = FalseStr :$name = '<anon>' --> Thread:D)

Creates and returns a new Thread, without starting it yet. &code is the code that will be run in a separate thread.

$name is a user-specified string that identifies the thread.

If $app_lifetime is set to True, then the thread is killed when the main thread of the process terminates. If set to False, the process will only terminate when the thread has finished.

class Version

From Version

(Version) method new

method new(Str:D $s)

Creates a Version from a string $s. The string is combed for the numeric, alphabetic, and wildcard components of the version object. Any characters other than alphanumerics and asterisks are assumed to be equivalent to a dot. A dot is also assumed between any adjacent numeric and alphabetic characters.