Technology
 

Loop

From Perl6 Wiki

The Perl6 equivalent of the Perl5 for statement, loop is also now the prefered way of doing infite loops.

Contents

[edit] Description

Loop takes a declaration which is lexically scoped, (it is not seen after the loop is done) and it takes a condition to check for in the main loop and then a function to do in each iteration although you can just append that to the end of statements yourself.

[edit] General Structure


loop( &declaration, &condition, &iteration ) { &statements }

[edit] Desugared Structure

{
    &declaration();

    loop {
        leave if &condition();
        &statements();		
        &iteration();
    }
}();

[edit] Examples

[edit] Multiplication Table

MultiplicationTable(12,12);

sub MultiplicationTable( Int $height, Int $width ) returns Bool {

    loop( my Int $y = 1; $y <= $height; $y++) {
        loop( my Int $x = 1; $x <= $width; $x++ ) {
	    [ ($x*$y), "\t" ].join.print;     # Separate each column by a tab character, \t
        }
        "\n".print();	
    }

    return ?(1);
}