CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: Throw Down The Gauntlet: Re: [mike.berding] Anagram Generator: Edit Log



FishMonger
Veteran

Apr 24, 2011, 9:30 PM


Views: 43451
Re: [mike.berding] Anagram Generator

Mike,

Did you realize that you were posting to a thread which is over 8 years old, and that the OP hasn't been back to this forum since asking for someone to provide the answer to this [his] homework assignment?

Looking over your code I see that you've use the strict and warnings pragmas, which is very good and often left out by others, but there are a few issues with your code.

This and most other programming forums provide "code tags" to be used to surround the code which retains the code indentation and offsets the code to make it easier to read. Please use those code tags. You should also add some vertical whitespace for the same reason.

The use of "goto" is almost never the proper choice for flow control; it create "spaghetti code". Proper use of subroutines and recursion is the more appropriate choice.

Your system call to clear the screen assumes that the user is running on Windows, which may or may not be the case. It would be better to use a more system independent approach.

Here's a rewrite of your script which could still use a couple improvements, but is cleaner and more up to today's standards.


Code
#!/usr/bin/perl 

use strict;
use warnings;


my $ClearScreen = $^O =~ /win32/i ? sub { system 'cls' }
: sub { system 'clear' };

CreateAcronym();

exit;

#########################

sub CreateAcronum {

$ClearScreen->();

my $prompt = "Enter the phrase you want to turn into an acronym.\n";
print $prompt, '-' x length($prompt);
chomp(my $phrase = <>);

my $acronym = join('', map { uc(substr($_,0,1)) . '.' } split " ", $phrase);

my $prompt2 = "The acronym for the phrase '$phrase' is:\n";
print "\n", $prompt2, '-' x length($prompt2), "\n$acronym\n\n";

CreateAcronym() if WantToPlayAgain();
}

sub WantToPlayAgain {
my $reply;
do {
print "Make another acronym? (y or n)\n";
chomp($reply = <>);

if ( $reply =~ /^y(es)?/i ) {
return 1;
}
if ( $reply =~ /^no?/i ) {
print "\nOkay! Have a good day! \n";
exit;
}
print "Please select Y or N (yes or no).\n";

} until $reply =~ /^y|n/i;
}



(This post was edited by FishMonger on Apr 24, 2011, 9:32 PM)


Edit Log:
Post edited by FishMonger (Veteran) on Apr 24, 2011, 9:32 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives