1. Why use say then add explicit new lines at the end of the line?
  2. Why a series of while loops, especially as $type isn't changed from one loop to the next?
  3. Think about what happens in each loop if the body of the if is not executed (hint - what causes the loop to terminate?)
  4. The string returned by <> includes the new line character at the end of the line. You probably want to chomp before using entered strings as values.
  5. Why don't you "use warnings"?
  6. shift isn't going to do it for you for "paralellogram".

For a version of your program to ponder a while consider:

use 5.010; use strict; use warnings; my %types = ( square => {dimensions => ['side'], calc => \&calcSquare}, rectangle => {dimensions => ['width', 'height'], calc => \&cal +cRect}, parallelogram => {dimensions => ['base', 'height'], calc => \&cal +cRect}, ); say "I am an area calculator."; while (1) { say "Enter stop to stop or one of: ", join ' ', sort keys %types; print "What type of shape am I working with? "; my $type = <>; chomp ($type); last if lc $type eq 'stop'; if (!exists $types{lc $type}) { say "Sorry, I don't know about $type."; next; } my @dims; for my $dim (@{$types{$type}{dimensions}}) { print "Enter a value for $dim:"; my $value = <>; chomp $value; push @dims, $value; } say "The area or your $type is: ", $types{$type}{calc}->(@dims); } say "Thanks for using me. Come again sometime"; sub calcSquare { return $_[0] * $_[0]; } sub calcRect { return $_[0] * $_[1]; }

Whenever you find yourself writing essentially the same code over and over, consider either using a loop or using a subroutine to contain the common stuff. This example use a hash which contains a reference to the subs used to do the actual calculation and a list of the dimensions required for each shape. Adding a new shape is just add the calculation sub and a new line to the hash.

True laziness is hard work

In reply to Re: What is wrong with this code? by GrandFather
in thread What is wrong with this code? by perl.j

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.