Tues Sep 9 Lecture Notes (We'll see how much of this we cover and where we go.) * Questions? * Ground rules * ==> ask questions <== to slow me down. Please don't be shy. * Many of my examples will be unix-ish. That's just what I'm most familiar with. Your mileage may vary. * This first part of the course - and the first three chapters - are trying to give you a taste to get you going; we'll go slower and more systematically in a bit. * The name of the game is ====> debugging. <==== Expect your programs to fail most of the time. That's normal. Your goal isn't to write perfect programs the first time; instead you're trying to learn to be a detective, finding out what's wrong by whatever it takes: trial and error, leaps of intuition, elimination of all other alternatives, or even peaking behind the curtain if necessary. * Running an editor and saving a text file * Note that ome window systems will hide extension (.txt, .pl, ...) * Do some demos in class. * Most of these editors will require some set-up; I haven't had time to get all the lab computers "just so." * XEmacs or Emacs : * All commands can be reached by typing their name after Meta-X. * Note particularly * Meta-X c-perl-mode * Meta-X font-lock-mode * "Meta-X" means either * escape-x, or * hold down meta key and type x * Don't type the whole command; press "tab" for auto-completion. * Type "control-h" for help. Then "t" for tutorial. * control-x 2 - splits the window vertically. * Meta-x shell - start a command shell within a window. * TextPad : * Perl mode needs to be "added". (I forget the menu item.) * Standard perl file endings are .pl and .pm * You can also run the compiler from within textpad. * perl -de1 * an idiom to get you to a place where you can type perl code and see it execute line by line. * (Do this in class.) * #!/usr/bin/perl - what's that all about? $ chmod +x program.pl (on unix) $ ./program.pl * Getting Help * at the command line: * "man perl" and the many others like "man perlfunc" * perldoc * online at www.perldoc.com * online at www.perlmonks.org - many tutorials and examples. * But please don't ask them to do your homework for you. * aside: plaigarism vs open source vs re-inventing the wheel * index in the back of the text * index in the Camel * email me : mahoney@marlboro.edu * How Computers Work - an overview * They work great :) (Have you seen the little sideways smiley before?) * Feel free to chime in; I'm not sure where you all are. * Ideas include * memory (various kinds) vs cpu vs disk "files" * bits, bytes, powers of 2 : kilo, mega, ... * clock speed and data bus * logic : and,or, and all that * data : int, float, ASCII char, string, ... * other models - parallel, analog, ... * Turing machine * ... there's lots to say here, we'll see how much we want to do. * How to write computer programs. * Think before you code. * Describe in writing the inputs and outputs. * Test everything. (Many people code the tests before the program.) * Comments and documention. * Header describing how it runs, who wrote it at the top of file. * readme.txt describing group of files and project * intellegent comments throughout on how it was implemented. * Style * Indentation * Variable names * Lucid prose * Debugging * Compile time vs Run time errors. * Don't assume! * What to do when it doesn't work * Stare at the code (and the errors). * Add lots of print statements to see what it's really doing. * Remove (comment out) pieces until it works. * Ask for help. * On to perl specific syntax * "nouns" (or data, or variables) have a funny character out front. Here's the complete list. $scalar holds an integer, float, string, or reference @list any (including non-uniform) collection of scalars also sometimes called an "array" %hash a collection of paired ( key => value ) associations &code one way to refer to a subroutine. (But usually the & can be omitted.) * Each sentence ends with ; * "verbs" (or function calls, or assignments) are things to be done to the nouns. * "comments" start with # and go to the end of the line. * Assignment. "=" means "stick the thing on the right into the thing on the left." It does *NOT* claim equality or test for equality. (Oops. This syntax is usually confusing for beginning programmers.) * Example $x = 5.3; # Put the value 5.3 into variable $x. $answer = sqrt($x); # Apply sqaure to 5.3, put result into $answer. print("The square root of 5.3 is $answer.\n"); * Why does this language use "$x" rather than just "x"? * Name space clarity. (More important than you might think.) * String interpolation. * Leaves more wriggle room in other aspects of syntax. (For example - parens after functions are optional.) * Note that even quotation marks "hi" are complicated verbs in Perl, with a lot going on behind the scenes. The quote has many variations, which we'll see in detail later. Here are a few examples to wet your whistle. $name = "Jim"; # define a scalar. print "My name is $name.\n"; # interpolation print qq{My name is "$name".\n}; # same, another way. print 'My name is $name.\n'; # no interpolation print q{"My name is "$name".\n}; # again, another way. print <