Reading and Writing Data Files Why do you need reading and writing data files in your CGI programming? Web server does not have permission to create file.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

● Perl reference
CGI Programming.
1 PHP Storing and Retrieving Data. string fgets(resource handle [, int length]) Reads and returns one line from a file; moves file pointer to next line.
CGI Programming Part 2. Input Tags Many different ways of getting data from the user. The tag is used most often. has a type attribute –Specifies the.
Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizard’s Guide to PHP by David Lash.
COMP234 Perl Printing Special Quotes File Handling.
Processing Form Commands in Perl for the data decoding from Web server: 1.substitute Syntax: $mystring =~ s/pattern/replacement/; This command substitutes.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Processing Form Commands in Perl for the data decoding from Web server: 1.substitute Syntax: $mystring =~ s/pattern/replacement/; This command substitutes.
And so on CGI programming Web Services Java Programs for the Web.
Building Secure Software Chapter 9 Race Conditions.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Guide To UNIX Using Linux Third Edition
Perl - Advanced More Advanced Perl  Functions  Control Structures  Filehandles  Process Management.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
The foreach LooptMyn1 The foreach Loop The foreach loop gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error.
Let’s Make An Form! Bonney Armstrong GD 444 Westwood College February 9, 2005.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
Perl Web Page – Just Enough Pepper. Web site Set up the top of your script to indicate perl and plain text #!/usr/bin/perl print "Content-type:text/plain\n\n";
Building PERL Scripts on a Windows system* *and running those scripts on an Apache server!
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Linux+ Guide to Linux Certification, Third Edition
9 1 DBM Databases CGI/Perl Programming By Diane Zak.
CGI Programming. What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. CGI is not a Perl-specific concept. Almost.
1 HTML Forms
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
1 CGI with perl. 2 References Perl tutorials and references:
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
Introduction to Unix – CS 21
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming (2) Ruibin Bai (Room AB326) Division of Computer Science The University.
Storing and Retrieving Data
Introduction to Programming the WWW I CMSC Winter 2003.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
CS 330 Class 9 Programming plan for today: More of how data gets into a script Via environment variables Via the url From a form By editing the url directly.
HTLM Forms CS3505. Form Handling in Browser html User Files out form WEbBROWSErWEbBROWSEr User read response submit Get URL?input html Get file html script.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
PHP-5- Working with Files and Directories. Reading Files PHP’s file manipulation API is extremely flexible: it lets you read files into a string or into.
HTML forms. Form elements  Forms consist of  Descriptions (explanation, instructions)  Places to enter information  free form (enter text)  check.
CS 330 Class 8 Homework A pattern that contains a word with an optional period A pattern that contains Fred with a space (not Freddy) See regexp.txt guest4.htm.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
The Scripting Programming Language
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
Linux Administration Working with the BASH Shell.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
1 CGI (Common Gateway Interface) CmpE 587 Emir Bayraktar Onur Bük.
Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
Programming Basics Web Programming.
How to create and run CGI programs using the CS web server.
File IO and Strings CIS 40 – Introduction to Programming in Python
Computer Science II First With files.
Computer Science II First With files.
Presentation transcript:

Reading and Writing Data Files Why do you need reading and writing data files in your CGI programming? Web server does not have permission to create file in your directory for safety reason. In order to write to a data file, you must usually make it world-writable, via the chmod command: chmod 666 myfile.dat This sets the permissions so that all users can read from and write to the file. The bad part about this is, it means that anyone on your system can go in and change your data file, or even delete it, and there’s not much you can do it.

Opening Files Reading and writing files is done by opening a filehandle, with the statement: open(filehandle, filename); The filename may be prefixed with a “>”, which means to overwrite anything that’s in the file now, or with a “>>”, which means to append to the bottom of the existing file. If both > and >> are omitted, the file is opened for reading only. Here are some examples: open(INF,"mydata.txt"); # opens mydata.txt for reading open(OUTF,">outdata.txt"); # opens outdata.txt for overwriting open(OUTF,">>outdata.txt"); # opens outdata.txt for appending The filehandles in these cases are INF and OUTF. You can use just about any name for the filehandle, but for readability, it’s always good to name it something relevant.

One problem with the above code is that it doesn’t check to ensure the file was really opened. The safe way to open a file is as follows: open(OUTF,">outdata.txt") or dienice("Can't open outdata.txt for writing: $!"); The $! in the above dienice message is a Perl variable that stores the error code returned by the failed open. Printing it out may help you figure out why the open failed.

File locking Why use file locking? CGI processes on a Unix web server can run simultaneously, and if two scripts try to open and write the same file at the same time, the file may be erased, and you'll lose all of your data. To prevent this, flock(OUTF,2) is used to exclusively lock the survey file while we are writing to it. (The 2 means exclusive lock.) Thelock will be released when your script finishes running, allowing the next CGI to access the file. This is only effective if all of the CGIs that read and write to that file also use flock; without flock, the CGI will ignore the locks of any other process and open/write/erase the file.

Reset the File Pointer Since flock may force the CGI to wait for another CGI to finish writing to a file, you should also reset the file pointer, using the seek function: seek(filehandle, offset, whence); offset is the number of bytes to move the pointer, relative to whence, which is one of the following: 0 beginning of file 1 current file position 2 end of file So, a seek(OUTF,0,2) ensures that you start writing at the very end of the file. If you were reading the file instead of writing to it, you’d want to do a seek(OUTF,0,0)to reset the pointer to the begin of the file flock is not supported on all systems

Closing Files When you’re finished writing to a file, it’s best to close the file, like so: close(filehandle); Files are automatically closed when your script ends, as well.

Reading Files Read one line at a time: open(INF,"survey.out") or dienice("Can't open survey.out: $!"); $a = ; # reads one line from the file #into the scalar $a close(INF); read the entire file into an array open(INF,"survey.out") or dienice("Can't open survey.out: = ; # reads the ENTIRE FILE into close(INF);

Example (How to read the entire file into an array) The following code shows how to read the entire file into an array, then loop through each element of the array to print out each line: open(INF,"survey.out") or dienice("Can't open survey.out: = ; close(INF); foreach $line { chomp($line); print "$line\n"; }

Example (survey.cgi) #!/usr/bin/perl print "Content-type:text/html\n\n"; read(STDIN, $buffer, = split(/&/, $buffer); foreach $pair { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/\n/ /g; # replace newlines with spaces $value =~ s/\r//g; # remove hard returns $value =~ s/\cM//g; # delete ^M's $FORM{$name} = $value; }

open(OUTF,">>survey.out") or dienice("Couldn't open survey.out for writing: $!"); # This locks the file so no other CGI can write to it at the # same time... flock(OUTF,2); # Reset the file pointer to the end of the file, in case # someone wrote to it while we waited for the lock... seek(OUTF,0,2); print OUTF "$FORM{'name'}|$FORM{‘address'}|";

close(OUTF); print <<EndHTML; Thank You Thank You! Thank you for your feedback. Return to our home page EndHTML sub dienice { my($msg) print " Error \n"; print $msg; exit; }

Some works need to do to write to your data file Enter your cgi-bin folder and create a file called survey.out: Touch survey.out Change the file permission to allow your cgi program to read and write to that file: chmod a+w survey.out