lesson 1 ======== #! /usr/bin/perl and comments ----------------------------- Normally, a line with the "#" character means the text following the "#" is ignored by the interpretor, i.e. a comment, e.g. $f = 2; # this is a comment However, if a line appears as above in the form #! pathname on the first line of the file, with the "#" in the first column, linux (and unix) look for the executable specified by the path name and use it to process the commands in the files. Other executables are the linux/unix shells, e.g. /bin/bash, or other command interpretors, e.g. /usr/bin/python. strings ------- strings are collections of ascii characters, e.g. "a", "b", "%", and non-printable characters such as CR (carriage return), TAB (tab), etc. strings are referenced with quotes, e.g. "jo-ann" "this is a string" "this is a string with a carriage return on the end\n" "this is a string\t with tab\t characters \t" The most command way to express the non-printable ascii characters in perl is with the defined constants: \n - carriage return \t - tab etc. variables --------- variables are storage element that store objects, e.g. numbers, strings, etc. variables in perl are referenced by "$" followed by a set of alphanumeric characters, idenifying the variable. $variable_name For example $acc $add_reg $joann $steve23456 $this is not an acceptable variable name $this is not a $#!@ing acceptable variable name variables that start with a number are usually special perl variables and should be avoided, e.g. $1 $2 arrays ------ arrays are collections of objects, e.g. numbers, strings, etc. arrays in perl are referenced as a list of objects, e.g. @reg, or a specific element of the array, e.g. $reg[0], using the square brackets "[" and "]". arrays are alway indexed from 0 to size-1, where size is the number of elements in the array. The number of elements of an array is assigned to the variable $#array_name, e.g. for the array @jo_ann, the number of elements in the array is contained in variable $#jo_ann. x ---- string x repeat_value This operator is used to repeat a string a specified number of times, e.g, as above "0" x 64 returns a string of 64 "0"s, i.e. the character "0" repeated 64 times. Other examples: "d" x 5 is "ddddd" "jo-ann" x 3 is "jo-annjo-annjo-ann" Note: this is not used as a statement, but as a value assigned to an object, e.g. a variable. = --- left_operand = righ_operand This operator assigns the value of the right operand to the left operand. The most common use is assigning a value to a variable or a list of values to an array, e.g. $acc = "0" x 64; $add_reg = "0" x 8; $add_reg = $add_reg + 1; The following initializes each of the first eight elements of the array @reg to a string of 64 "0"s: @reg = ( "0" x 64 ,"0" x 64 ,"0" x 64 ,"0" x 64 ,"0" x 64 ,"0" x 64 ,"0" x 64 ,"0" x 64 ); What appears between the "(" and ")" is a list of items which correspond to the elements of the array @reg. So $reg[0] is "0000000000000000000000000000000000000000000000000000000000000000" $reg[1] is "0000000000000000000000000000000000000000000000000000000000000000" ... $reg[7] is "0000000000000000000000000000000000000000000000000000000000000000" Note: and assignment with "=" usually constitutes a statement and must be ended with a semicolon. < --- left_operand < right_operand This operator returns the value true if the left operand is less than the right operand. False otherwise. Other operators are numerica comparison operators == 5 == 5 true != 7 != 2 true < 7 < 4 false > 7 > 4 true <= 7 <= 11 false >= 11 >= 11 true string comparison operators eq "hello" eq "hello" true ne "hello" ne "hello" false lt "hello" lt "aaaaa" false gt "hello" gt "aaaaa" true le "hello" le "aaaaa" false ge "aaaaa" ge "aaaaa" true Caveat: "==" and "eq" are not the same as "=". See the "if" statement below. +, ++ ----- left_operand + right_operand left_operand ++ The "+" operator adds the right operand to the left operand. The "++" operator adds 1 the left operand. It is equivalent to left_operand = left_operand + 1 $i = $i + 43556; $i = $i + $j; $i++; commands/statements ------------------- In general, perl commands end with the semicolon or a right curly bracket "}" e.g. $acc = "0" x 64; # assign a string of 64 0s to the variable $acc. print "hi there\n"; A semicolon by itself is also a command, but does nothing and is referred to as "the null command". open ---- All files must be opened before access, either reading or writing. The "open" function has the following format: open(file_handle, file mode and file name) open returns a nil value if the file cannot be opened, for whatever reason. Each of the above modes can also be prefixed with the + character to allow for simultaneous reading and writing, e.g. open(MEM_DA,"+ ✓ ✓ append >> ✓ read/write +< read/write +> ✓ ✓ read/append +>> ✓ The file handle is a logical name used to access the file in other commands, e.g print (see below). File handles can be any alphanumeric string and are traditionally written in upper case letters for readablility, but can be lower case as well. close ----- File that are opened for access should be closed when the perl script is completed with close file_handle e.g. close MEM_DA; print ----- Print to the screen. Some examples: print "hello world" with a carriage return print "hello world\n"; print "add_reg ", the value of the variable $add_reg and a carriage return. print "add_reg $add_reg\n"; print "reg_" the value of the variable $i, the value of the element of array @reg indexed by the variable $i and a carriage return. print "reg_$i $reg[$i]\n"; print the string "hello world\n" to the file pointed to by file handle EXAMPLE_FILE print EXAMPLE_FILE "hello world\n"; unless ------ unless (open(MEM_RW,">mem_rw.ffo")){ print "Can't open file mem_rw.ffo\n"; } In English, this command means that "unless the file mem_rw.ffo can be opened, print the message "Can't open file mem_rw.ffo" to the screen and add a carriage return "\n", otherwise open the file for reading (>)". You could also write it this way if (open(MEM_RW,">mem_rw.ffo")){ # do nothing since the file is opened. } else { print "Can't open file mem_rw.ffo\n"; } if ... ------ if (condition) { statements } Executes statement when condition is true. Note: Commands are also referred to as "statements" as in "the if statement". Note that the "if statement" above is opened with a left curly bracket "{" and closed with a right curly bracket "}". A semicolon is not required. Examples: if ($i < 4) { print "$i\n"; } print "$i\n"; is only executed with the numerical value of the variable $i is less than to 4. Perl does allow "=" in a conditional statement: if ($i = 2) { print "$i\n"; } In this case, $i is assigned the value 2 which returns a true value. This means the command print "$i\n"; will always be executed. However, in the following if ($i == 2) { print "$i\n"; } print "$i\n"; is only executed with the numerical value of the variable $i is equal to 2. <> (read) --------- Returns the next line from the object pointed to by the file_handle, e.g. This can be used to store the line read to a variable: $ad = ; chop ---- chop(variable) A function that removes the last character of a string. For example chop($rw); in dr_shred_mem.pl, removes the carriage return from $rw. for loops ========= for ($i=0;$i<$#reg;$i++){ print "reg_$i $reg[$i]\n"; } Executes the commands between the curly brackets while the variable $i is less than the number of elements in the array @reg. On the first iteration of the loop, $i = 0. After every iteration of the loop, $i is incremented.