{ |one, step, back| }

The Shape Example in Perl6

Contributed by Jim Weirich

Corrections provided by Damien Conway.

Code for Perl6

File: shapes.pl

    class Shape {
        has $.x;
        has $.y;
        method moveTo($newx, $newy) {
            $.x = $newx;
            $.y = $newy;
        }
        method rMoveTo($newx, $newy) {
            .moveTo($.x + $newx, $.y + $newy);
        }
    }

    class Rectangle is Shape {
        has $.width is rw;
        has $.height is rw;
        method draw() {
            print "Drawing a Rectange at:($.x,$.y), width $.width, height $.height\n";
        }
    }

    class Circle is Shape {
        has $.radius;
        method draw() {
            print "Draw a Circle at:($.x,$.y), radius $.radius\n";
        }
    }


    @scribble = (Rectangle.new(x=>10, y=>20, width=>5, height->6), Circle.new(x=>15, y=>25, radius=>8));

    for @scribble -> $ashape {
        $ashape.draw;
        $ashape.rMoveTo(100, 100);
        $ashape.draw;
    }

    $arectangle = Rectangle.new(x=>0, y=>0, width=>15, height=>15);
    $arectangle.width = 30;
    $arectangle.draw;