#!/usr/bin/perl -w

use Term::ANSIColor;
use strict;

# clear the screen to start off
system ('clear');

print   color("red"),    "###############################################################",color("reset"),"\n",
        color("white"),  "Presenting:                                                    ",color("reset"),"\n",
        color("white"),  "       Exersize  8.6                                           ",color("reset"),"\n",
        color("red"),    "###############################################################",color("reset"),"\n";
print   color("yellow"), "Program will input height and width from the user              ",color("reset"),"\n";	
print                    "Part 1\tWrtie subroutine that calcuates area of a sqare.       ",color("reset"),"\n",
	color("magenta"),"\tSub takes 2 arguments:\tHight and width                      ",color("reset"),"\n",
	color("magenta"),"\tSub returns 1 string:\tArea                                  ",color("reset"),"\n";
print                    "Part 2\tModify the sub in Part 1 to also return the perimeter. ",color("reset"),"\n", 
	color("green"),  "\tSub  takes 2 arguments:\tHight and width                     ",color("reset"),"\n",
	color("green"),  "\tSub returns 2 strings:\t Area and Perimeter                  ",color("reset"),"\n";

sub calculate_area {
	my $height = shift @_;
	my $width = shift @_;
	my $area = ($height*$width);
	return ( $area );
}

sub calculate_area_and_perimeter {
	my ($height2, $width2) = @_;
	#cacluate area
	my $area2 = ($height2*$width2);
	
	# calculate perimeter
	my $perimeter = 2*($height2+$width2);
	return ( $area2, $perimeter );
}

sub valid_input {
	my $dimention= shift @_;
	print color("yellow"),"Input the $dimention:\t",color("reset");
	chomp ( my $input_number=<STDIN>);

	# perl cookbook pg 44: Recipe 2.1 Check Whether a string is a Valid Number
	# Allow a + at the beginning, like +2
	# Allow a decimal point if number started with a digit  like 0.2 or 3.14
	# and Allow a whole number, like 3
	unless ( $input_number =~ /^\+?\d\.?\d*$/ ){
		die "Error: $input_number is not a positive number.\n";
	} 
	return ($input_number);
}

# input the height
my  $dimention="height";
my $height = &valid_input($dimention);

# input the width
my $dimention="width";
my $width = &valid_input($dimention);

my $area=( &calculate_area($height, $width) );
print "\n",color("magenta"),"Part1.\tWith a Height=",color("reset"),"$height",
      color("magenta")," and Width=",color("reset"),"$width", 
      color("magenta")," the Area=",color("reset"),"$area\n";

my ($area2, $perimeter)=( &calculate_area_and_perimeter($height, $width) );
print "\n",color("green"),"Part2.\tWith a Height=",color("reset"),"$height",
      color("green")," and Width=",color("reset"),"$width", 
      color("green")," the Area=",color("reset"),"$area2",
      color("green")," the Perimeter=",color("reset"),"$perimeter\n";
