home -- outline -- lectures -- assignments -- discussion -- tips -- links --



Wednesday Feburary 18

CGI & Perl (cont.)


Here are some simple tips on how to start your programming assignments:

  • Start small!

    Always start with a simple task that you can understand. If you start your assignments trying to do everything at once, you're bound to write broken code that you can't possibly debug. For this assignment, make a basic CGI that just prints out a page. Start with the lecture from last Wednesday and move forward from there. Make sure you understand what's going on at each step: how can you fix something if you don't understand how it works in the first place?

  • Add one thing at a time!

    Once you've got the basics down, and your CGI spits out a simple page, you're ready to move on. Break the assignment up into sub-tasks, and start with the first, most basic task. Don't decide to add a complicated form and file writing capabilities andfile reading all at once! Add a simple form, then move on to bigger and better things. This way, if something breaks, you don't have to do too much back-tracking to figure out what went wrong.

  • Save a copy!

    When you get one of your tasks completed, don't jump right in and start editing the CGI! Save a copy so when you break the next version (and trust me, you will!) you have something to compare you new code against. This makes it much easier for me to look at your code and see where you were and where you are going.

  • Don't let the errors get you down!

    Errors are an important part of the process. If you are using a debugger or compiler with your programs, you will often get several errors if the code is broken. The first error will cause the program to get confused, and then following lines of code will be misunderstood, and you see a long list of errors. Don't get depressed! Usually, if you find the problem that made the very first error, your list will get much shorter or go away altogether! Don't be confused by strange errors: usually you missed a ; or left out a " mark.

Here are some things you should remember about Pearl in general:
  • # mark is for comments

    This comments out an entire line of code, so that the program ignores the code altogether. If you have a line that is generating an error, use the # to comment it out and see if the program will run without it.

  • Carriage returns are a problem

    If you have a carriage return in a line of Pearl, it will break. Leave long lines to run over the end of the page. If you are using BB EDIT, you can use the "Soft Wrap Text" option in the features menu (second little purple square from the Left at the top of your window) to keep the lines from running off your page. Don't put "hard" returns in a line of Pearl...it generates errors.

  • " marks are a problem

    If you are using Pearl to print out a line of text that has " marks in it, like:

    print " < body bgcolor=lightblue text="#000000" link="#006633" > \n ";

    Pearl will choke on the middle quotes. This is beaus Pearl starts reading from the left, and reads that the PRINT statement goes from " to ". It starts at "<and moves to text=" and then starts trying to find the next command. This is bad, and causes Pearl to break.

    You can fix this by using qq+ and +; to replace the " marks at the beginning and end of your entire statement. This tells Pearl to use the + character as the delimiter (marker) for the code instead of a " mark. The resulting code looks like:

    print qq+ < body bgcolor=lightblue text="#000000" link="#006633" > \n +;