Routines

Common Perl 6 routines that are defined outside any class

See Sub on how to define routines and work with them.

Control routines

Routines that change the flow of the program, maybe returning a value.

sub exit

Defined as:

sub exit(Int() $status = 0)

Exits the current process with return code $status or zero if no value has been specified. The exit value ($status), when different from zero, has to be opportunely evaluated from the process that catches it (e.g., a shell); it is the only way to return an exit code different from zero from a Main.

exit prevents the LEAVE phaser to be executed, but it will run the code in the &*EXIT variable.

exit should be used as last resort only to signal the parent process about an exit code different from zero, and not to terminate exceptionally a method or a sub: use exceptions instead.

done

Defined as

sub done(--> Nil)

If used outside any supply or react block, throws an exception done without supply or react. Within a Supply block, it will indicate the supply will no longer emit anything. See also documentation on method done.

my $supply = supply {
    for 1 .. 3 {
        emit($_);
    }
    done;
}
$supply.tap-> $v { say "Second : $v" }done => { say "No more" });
# OUTPUT: OUTPUT: «Second : 1␤Second : 2␤Second : 3␤No More␤» 

The block passed to the done named argument will be run when done is called within the supply block.

emit

Defined as

sub emit(\value --> Nil)

If used outside any supply or react block, throws an exception emit without supply or react. Within a Supply block, it will add a message to the stream.

my $supply = supply {
  for 1 .. 10 {
      emit($_);
  }
}
$supply.tap-> $v { say "First : $v" });

See also the page for emit methods.

undefine

Defined as:

multi sub undefine(Mu    \x)
multi sub undefine(Array \x)
multi sub undefine(Hash  \x)

DEPRECATED in 6.d language version and will be removed in 6.e. For Array and Hash, it will become equivalent to assigning Empty; for everything else, equivalent to assigning Nil, whose use is advised.