Having puttered about with Perl just long enough to have a general idea of what's going on, I've achieved a reasonable level of competentcy.

But, of course, that's not good enough

Below is a simple prime number program I wrote not long ago. What I'd like is for you illustrious monks to go over it and criticize (mock?) EVERYTHING. No matter how small or trivial. Is there a more efficient way to do $THING? Can my code be made easier to read / terse / faster / better smelling? Add features? Remove bloat?

You get the idea. Nitpick! Educate! Correct! (And a free ++ from me for each helpful comment, no matter how vicious)

#!/usr/local/bin/perl -w use strict; #These variables are user-configurable. my $countermax = 10000; #The max number of primes per file. ( +To regulate file size) my $primefile = "Primes.seed"; #The location of the seed file. This + is needed. my $path = "./"; #The first part of the path to dump the files in. + This must be pre-existing. my $last = ".txt"; #The extension to append to the file +names. Your choice. my $beginloc = "00000"; #The number to start counting at whe +n making filenames. #End users should not edit below this line my $lowrange = 0; my $hirange = 0; my $current = 0; my $i = 0; my $primey = 0; my $counter = 0; my @parray; my $outputloc = $beginloc; #First this looks for a pre-existing primes file #which has all the primes less than 10000 in it. #It needs this file. Assume it is present or #suggest a better way or something... open PRIMES, $primefile; while(<PRIMES>) { push (@parray, $_); } close PRIMES; open RESULTS, ">".$path.$outputloc.$last; print "Welcome to the prime number program.\nThis program will tell yo +u every prime number within a certain range.\nPlease enter the lower +range now: "; $lowrange = <STDIN>; chomp $lowrange; #Simple idiot-proofing. I didn't bother to check to make #sure it's actually an integer. What's a good way to #do this? if($lowrange < 0) { $lowrange = 0; print "\nYour number must be positi +ve. I have set it to 0 for you."; } print "\nHow high should the program search for primes? "; $hirange = <STDIN>; chomp $hirange; if($hirange <= $lowrange) { $hirange = $lowrange + 100; print "\nYour second number must be at least equal to the first. \nI h +ave arbitratily set it to be 100 greater.\n"; } print "Now computing the range of all primes between $lowrange and $hi +range. \nPlease wait.\n"; #No need to check even numbers, right? if($lowrange % 2 == 0) { $lowrange++; } #Of course, since 1 isn't a prime, we have to account #for that. Mathmaticians, LART me if I'm wrong. if($lowrange == 1) { print RESULTS "2\n"; $lowrange = 3; $counter++; } for($current = $lowrange; $current <= $hirange; $current += 2) { $primey = 1; for($i = 0; $parray[$i] <= sqrt($current); $i++) { if($current % $parray[$i] == 0) { $primey = 0; $i = 10000; } } if($primey == 1) { $counter++; #print "PRIME FOUND: $current\n"; print RESULTS "$current\n"; if($counter%$countermax == 0) { close RESULTS; $outputloc++; print "\nFile full. Currently ".($current *100/$hirange)."\% done."; open RESULTS, ">".$path.$outputloc.$last; } } } print "\nTotal amount of primes: $counter \n"; print "Your results have been saved, starting with ".$path.$beginloc.$last."\n\n";

In reply to Mock my code! by Superlman

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.