More CGI programming.... Back to CGI programming... Now that we know how to use conditional...

Click here to load reader

  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    1

Embed Size (px)

Transcript of More CGI programming.... Back to CGI programming... Now that we know how to use conditional...

  • Slide 1
  • More CGI programming...
  • Slide 2
  • Back to CGI programming... Now that we know how to use conditional expressions, we can write a CGI program that determines whether the environment variables set by the HTTP server demon include one of interest
  • Slide 3
  • CGI program which checks for a particular env var #!/usr/local/bin/perl print May, Bob=>Ann, Tim=>Una); delete( $mothers{Bob} ) Can programmer-defined subroutines take arguments? Yes, although the way in which they handle arguments is a little different from what you are used to">
  • Passing Arguments to subroutines The subroutines which we have defined so far have not taken any arguments Pre-defined Perl subroutines can take arguments, as in this program fragment: %mothers = (Tom=>May, Bob=>Ann, Tim=>Una); delete( $mothers{Bob} ) Can programmer-defined subroutines take arguments? Yes, although the way in which they handle arguments is a little different from what you are used to
  • Slide 13
  • Passing Arguments to subroutines (contd.) Suppose we want a subroutine called greetPerson which takes one argument, a string, and prints a message greeting the person whose name is the string An example call might be greetPerson(Eamonn de Valera) which should produce the output Hello, Eamonn de Valera The following program fragment should produce the same output: my $person = Eamonn de Valera; greetPerson($person) How would we define such a subroutine?
  • Slide 14
  • Passing Arguments to subroutines (contd.) Your first instinct might be to write something like this: sub greetPerson($formalArgument) { print Hello, $formalArgument } but that would be WRONG A subroutine in Perl must access its actual argument(s) through a special array variable called @_ Since our subroutine takes only one argument, this would be in the first element of @_, so our definition would be: sub greetPerson { print Hello, $_[0] }
  • Slide 15
  • Passing Arguments to subroutines (contd.) Suppose we want a subroutine called greetTwoPeople which takes two string arguments and prints a message greeting the people whose names are the strings An example call might be greetTwoPeople(Eamonn, Michael) which should produce the output Hello, Eamonn and Michael Since our subroutine takes two arguments, these would be in the first two elements of @_, so our definition would be: sub greetTwoPeople {print Hello, $_[0] and $_[1]}
  • Slide 16
  • Passing Arguments to subroutines (contd.) Suppose we want a subroutine called greetMember which takes two arguments an array of strings an integer pointing to one member of this array and prints a message greeting the person whose name in the indicated string An example use is: @club = (Eamonn, Michael, Harry); greetMember(2, @club) which should produce the output Hello, Michael This introduces a further complication...
  • Slide 17
  • Passing Arguments to subroutines (contd.) All actual arguments to a subroutine are collapsed into one flat array, the special array @_ Thus, the program fragment @club = (Eamonn, Michael, Harry); greetMember(2, @club) causes the subroutine greetMember to receive an @_ whose value is (2, Eamonn, Michael, Harry) So our definition would be: sub greetMember { print Hello, $_[$_[0]] }
  • Slide 18
  • Using local variables in subroutines Local variables can be defined in subroutines using the my construct Indeed, doing so enables us to write subroutines which are easier to understand subroutine greetMember on the last slide is clearer if it written using local variables, as follows: sub greetMember {my ($position, @strings); $position = $_[0]-1; @strings = @_[1..scalar(@_)-1]; print Hello, $strings[$position] }
  • Slide 19
  • CS 4400 got to here on 1 February 2002
  • Slide 20
  • Using local variables in subroutines We dont have to declare the local variables in a separate line We can just use the my construct in the statements where the vars first appear The subroutine greetMember on the last slide could also be written as follows: sub greetMember {my $position = $_[0]-1; my @strings = @_[1..scalar(@_)-1]; print Hello, $strings[$position] }
  • Slide 21
  • Using local variables in subroutines We can also use a subroutine called shift() to remove the first element from @_ Since shift() also returns, as its value, the value of the removed element, we can use it in an assignment statement Since have removed the first element, we can then assign the new value of @_ to @strings The subroutine greetMember on the last slide could also be written as follows: sub greetMember {my $position = shift(@_); my @strings = @_; print Hello, $strings[$position] }
  • Slide 22
  • Using local variables in subroutines What I regard as an unfortunate feature of Perl is that it allows a lot of abbreviations I present one here, simply because you will often see it in script archives if no explicit argument is given to shift() in a subroutine, it is assumed to be @_ Thus, in a script archive, you might find subroutine greetMember on the last slide written as follows: sub greetMember {my $position = shift; my @strings = @_; print Hello, $strings[$position] }
  • Slide 23
  • subroutines which return values We often need to define subroutines which return values, as in the following program fragment: my @numbers = (1, 2, 3, 4, 5); my $average = sum( @numbers ) / scalar( @numbers ); print $average It can be defined as follows: sub sum { my @numbers = @_; my $sum = 0; foreach my $value ( @numbers) { $sum = $sum + $value } return $sum } The value returned is specified with a return statement
  • Slide 24
  • subroutines which return values (contd.) A subroutine can contain more than one return statement The following program fragment defines and uses a boolean subroutine which checks for the existence of the argument passed to it if ( present ( $ENV{"EDITOR"} ) ) { print "\n The envVar EDITOR exists" } else { print "\n The envVar EDITOR does not exist" }; sub present { my $varInQuestion = $_[0]; if ( $varInQuestion ) { return 1 } else { return 0 } } It enables us to write a cleaner version of a CGI program we wrote earlier
  • Slide 25
  • Revised CGI program which checks for an env var (part 1) #!/usr/local/bin/perl print
  • Look-behind checks (contd.) (?
  • Slide 90
  • Back to CGI programming...
  • Slide 91
  • Watching out for hackers (contd.) This is the final definition of separateAndPrintDataIn() sub separateAndPrintDataIn {my (@equations, $name, $value); @equations = split("&",$_[0]); foreach my $equation (@equations) { ($name,$value) = split("=",$equation); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-f0-9])/pack("C",hex($1))/eg; if ( $value =~ m/ / ) {print SSI removed from following: "; $value =~ s/ //g }; print $name = $value ">
  • Slide 100
  • Improved program reporting GET method data Using this new definition of separateAndPrintDataIn() we have an improved version of the CGI program which is called by a HTML FORM and sends back to the browser a HTML page which lists the data it received from the form
  • Slide 101
  • Improved GET data program (part 1) #!/usr/local/bin/perl print customerFile.txt) opens a file called customerFile.txt and associates with it the handle Cus">
  • File Processing (contd.) Example usage: open(Customers,>customerFile.txt) opens a file called customerFile.txt and associates with it the handle Customers; usage of > explicitly states that we want write-only access Example usage: open(Customers,>>customerFile.txt) opens a file called customerFile.txt and associates with it the handle Customers; usage of >> explicitly states that we want append-only access
  • Slide 112
  • File Processing (contd.) When a program is finished reading from or writing to the device associated with a file handle, the channel to the device should be closed This is done by using the close() subroutine; this takes only one argument, a file handle Example usage: close( Customers )
  • Slide 113
  • Example Program Consider this program fragment: open(handle1, >output.txt); print(handle1 Hello, world!\n); print(handle1 How are you?); close(handle1) It places the following content in file output.txt: Hello, world! How are you?
  • Slide 114
  • Reading from a file We already know how to write to a file we use the print() subroutine, quoting the file handle To read from a file, we apply the input operator to the file handle Example usage: $line = This reads the next available line from the file which is associated with the handle myHandle99 and copies it into the scalar variable $line The input operator returns the special value undef at the end of a file
  • Slide 115
  • Example Program Consider this program fragment: open(myHandle,"output.txt"> output.txt") ) ) { print File unavailable } else ">
  • File Locking in Perl (contd.) Example fragment of file-writing program: if ( not (open(myHandle,>output.txt") ) ) { print File unavailable } else { flock(myHandle,2);... Write stuff to the file... flock(myHandle,8) }
  • Slide 127
  • File Access Permissions for CGI programs Remember that, in a multi-user operating system, different users have differing permissions to access a data file some users may be able to write to the file other users may be abl