Andrew_Levenson has asked for the wisdom of the Perl Monks concerning the following question:

I just started learning Perl; this is the first program i've ever tried to write. I can't really figure out what i'm doing wrong (one, because I don't know, and two, because my interpreter won't stay open long enough to tell me; it closes right when the program finishes). Can anyone help me figure out what i'm doing wrong? This is supposed to find primes between two numbers. (And if you could help with the interpreter issue, that'd be nice, too).
<code> use strict; use warnings; #finds prime numbers between two points; print "Please enter the values for the 2 numbers between which you wis +h to find the primes.\n"; $x=<STDIN>; chomp($x); print "\n"; #user inputs point x; $y=<STDIN>; chomp($y); print "\n"; #user inputs point y; for($i==$x; $i==$y; $i++){ $even=0; $notprime=0; $j=$i/2; if(int($j)==$j){ $even=1; # tests to see if it is even; } else{ $n=$i**(1/2); for($m=3; $m=$n; $m+=2){ $o=$i/$m; if(int($o)==$o){ $notprime=1; } } } if($even==0 && $prime==0){ print "$i is prime!\n"; } } $q=<STDIN>; # stops the program from closing...;
If anyone wants to help me figure out what's wrong... just let me know what you did? Thanks.

Replies are listed 'Best First'.
Re: Debugging?
by ikegami (Patriarch) on Mar 03, 2006 at 17:41 UTC

    Just for starters,

    • To see the errors, run the program from a command prompt:
      Start | Run | cmd
      From the command prompt:
      perl script.pl

    • You should really put
      use strict;
      use warnings;
      at the top of your program. This will save you lots of grief, even though you'll have to start declaring your variables (using my).

    • You still have the for wrong. I suggest you re-read the documentation.
      for ($i==$x; $i==$y; $i++)
      should be
      for (my $i=$x; $i==$y; $i++)
      (my added as a bonus)
      Or if you want to include $y,
      for (my $i=$x; $i<$y; $i++)

      But why not use the easier to read/understand/write
      for my $i ($x..$y-1)
      Or if you want to include $y,
      for my $i ($x..$y)

    • Similarly,
      for ($m=3; $m=$n; $m+=2)
      should probably be
      for (my $m=3; $m<$n; $m+=2)

    • By the way, you're assuming (i.e you don't validate) that the user supplies integers for $x and $y, and that the number he supplied for $x is smaller than or equal to $y.

Re: Debugging?
by rinceWind (Monsignor) on Mar 03, 2006 at 17:37 UTC

    You say "my interpreter won't stay open long enough to tell me; it closes right when the program finishes"

    I guess you are running the program by double clicking the .pl file. Try opening a command (cmd) window and running the program there.

    See also perldoc perldebug

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: Debugging?
by runrig (Abbot) on Mar 03, 2006 at 17:39 UTC
    Your use strict line is causing all sorts of helpful error messages to spew forth, but you are not seeing the error messages, because the $q=<STDIN> trick doesn't work with compile time errors. First, open a command line window and run the program from there, don't just click on the file. Then check out Use strict and warnings and the links from it, and the strict documentation on how to fix those errors. Mostly you will be putting my (see that also) in front of the first occurance of every variable.

    Also, when you're done fixing your problems, see -Mouse on an alternative way of doing that $q=<STDIN> thing (which has nothing to do with your problem but it's neat anyway).

Re: Debugging?
by zentara (Cardinal) on Mar 03, 2006 at 17:55 UTC
    Definitely learn how to use ptkdb the Tk front end to the perl debugger. Google for it, and you will find a number of good tutorials on how to use it. It allows you to graphically watch variables as you step thru your program. (P.S. Don't forget to save your watchpoint-setups for each file, else you need to redo them everytime you open the file).

    I'm not really a human, but I play one on earth. flash japh
Re: Debugging?
by smthames (Initiate) on Mar 03, 2006 at 17:46 UTC
    I'm on my way out so I will look at your code later. The first thing you want to know about is the perl debugger. It is activated by:
    /usr/bin/perl -d <script name>
    It has all the features of a standard debugger. Look at http://perldoc.perl.org/perldebtut.html. Good luck!