# shapes -- A simple shapes example # # Copyright (C) 1998, Bradley M. Kuhn # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # Please request a copy of the GNU General Public License from the author, # or retrieve it from http://www.gnu.org/copyleft/gpl.html # Alternatively, you can obtain a copy by writing to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # # Modified for use in COMP 331, Intermediate Perl 3/31/02 - C.D.Lane use warnings; use strict; use Circle; use Rectangle; # package main; sub DoSomethingWithShapes { my @shapes = @_; foreach my $shape (@shapes) { $shape->draw(); $shape->relativeMoveTo(100, 100); $shape->draw(); } } ############################################################################### print "Do something with Shapes:\n\n"; my $rect1 = new Rectangle(10, 20, 5, 6); my $circle = new Circle(15, 25, 8); my @shapes = ($rect1, $circle); &DoSomethingWithShapes(@shapes); ############################################################################### print "\nAccess a rectangle specific function:\n\n"; my $rect2 = new Rectangle(0, 0, 15, 15); $rect2->setWidth(30); $rect2->draw(); ############################################################################### print "\nTry something that only circles can do:\n\n"; push(@shapes, $rect2); # Use can() method so we don't crash on those shapes that don't support method foreach my $shape (@shapes) { # Set the radius, if this object happens to support that method $shape->setRadius(25) if $shape->can("setRadius"); $shape->draw(); }