![]() |
Web Conference 2004Writing Perl/CGI Scripts for ITS/ASET Web servicesUsing Data files |
| <- Back - Input and Output | | | Up | | | SQLite - Next -> |
One of the first things you will want to do with a dynamic content Web page is to save data on the server for later retrieval. There are various ways to do this, including databases. But first let's start with the oldest and simplest way, using files.
First let's read a file and send it to the browser.
#!/usr/local/bin/perl
print "Content-Type: text/html\n\n";
open(FILE,"example.html") or webdie("Cannot open example.html: $!");
while(<FILE>){
print;
}
close(FILE);
exit 0;
sub webdie {
print "<html>\n";
print "<body>\n";
print "<p>Error: $_[0]</p>\n";
print "</body>\n";
print "</html>\n";
exit 1;
}
|
Writing a file is similar to reading a file. You use the open() function, but with the > symbol in the filename to indicate you want to write to it. Then use print FILE to print to the FILE filehandle.
#!/usr/local/bin/perl
print "Content-Type: text/html\n\n";
$datafile = "write2file.dat";
open(FILE,">$datafile") or webdie("Cannot open $datafile for writing: $!");
print FILE "Check, check...is this mic on?\n";
close FILE;
# now tell the browser what you did
print <<END;
<html>
<body>
<p>$datafile was written</p>
</body>
</html>
END
exit 0;
sub webdie {
print "<html>\n";
print "<body>\n";
print "<p>Error: $_[0]</p>\n";
print "</body>\n";
print "</html>\n";
exit 1;
}
|
Check, check...is this mic on? |
Setting permissions to allow test.scripts.psu.edu to write filesTo give the script server the ability to save changes to individual files that already exist, do the following:
|
|
To give the script server the ability to create and change any file in a folder, do the following:
It is wise to grant these permissions on a separate folder than the folder where CGI scripts reside to prevent them from being altered by someone else using the servers. |
Let's do a simple log script. Save the parameters as they come and the time and IP address.
#!/usr/local/bin/perl
use CGI qw(:param);
print "Content-Type: text/html\n\n";
$logfile = "log2file.log";
open(FILE,">>$logfile") or webdie("Cannot open $logfile for appending: $!");
# localtime() - get today's date, in our local timezone
#
# 0 1 2 3 4 5 6 7 8
# ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
#
# $year - is number of years since 1900
# $mon - is number of months after January
# Other fields are in the format we want.
@localtime = localtime();
# this is the same as $localtime[5] = $localtime[5] + 1900;
$localtime[5] += 1900;
# this is the same as $localtime[4] += 1;
$localtime[4] ++;
# $datestamp - what is today's date, in YYYY/MM/DD-HH:MM:SS format
#
# sprintf("format","var1","var2"...) - output a scalar in the prescribed format
# %d - output the next variable in the list as a digit
# %2d - same as %d, but if less than 10, add a leading space
# %.2d - same as %d, but if less than 10, add a leading 0
# %4d - same as %2d, but save room for 4 digits, not just 2
$datestamp = sprintf("%4d/%.2d/%.2d-%.2d:%.2d:%.2d", $localtime[5],
$localtime[4], $localtime[3], $localtime[2], $localtime[1], $localtime[0]);
# you can also do the above with:
$datestamp = sprintf("%4d/%.2d/%.2d-%.2d:%.2d:%.2d", reverse @localtime[0..5] );
# log the time
print FILE "$datestamp: ";
# log the IP address
print FILE "$ENV{'REMOTE_ADDR'} ";
# log the paramters sent
foreach $parameter (param()) {
print FILE "Parameter($parameter) = \"" . param($parameter) . "\", ";
}
print FILE "\n";
close FILE;
# now tell the browser what you did
print <<END;
<html>
<body>
<p>Your parameters have been logged</p>
</body>
</html>
END
sub webdie {
print "<html>\n";
print "<body>\n";
print "<p>Error: $_[0]</p>\n";
print "</body>\n";
print "</html>\n";
exit 1;
}
|
Run log2file_clean.cgi [download]
| <- Back - Input and Output | | | Up | | | SQLite - Next -> |
If you have any questions, feel free to ask me - mailto:jcd@psu.edu
Content by: Jeff D'Angelo <jcd@psu.edu> © 2004
Last update on: Monday, 14-Jun-2004 14:32:50 EDT