#!/usr/local/bin/perl
# a very simple example to display
# query string from a url
print "Content-type: text/plain\n\n",
"QUERY_STRING:\n";
@inarray=split(/&/,$ENV{'QUERY_STRING'});
foreach (@inarray) {
print "$_\n";
}
This will produce the following output in your web browser:
QUERY_STRING: param=one this=thatYou'll notice that the use of ? and & and other special symbols are defined in Internet standards documents. (Building a fully-featured parser for the CGI parameters is outside the scope of this article, however.)
#!/usr/local/bin/perl
# an example of spitting out a jpeg file
$whichfile=$ENV{'QUERY_STRING'};
print "Content-type: image/jpeg\n\n";
open FILE, $whichfile;
while (read FILE, $f, 16384) {
print $f;
}
close FILE;
In this case, using some HTML code like the following will
display an arbitrary image file. (Of course, this is just
a silly example. You can undoubetly come up with a situation
where this might actually be useful...)
<html><body> <img src="/cgi-bin/imgscript.pl?bestimage.jpg"> </body></html>If, however, the user tries to save that image (by right-clicking on it in the web browser), the web browser will probably prompt for a name like "imgscript.pl" when saving the image. That isn't really a good choice for the filename, and in Windows, that .pl extension doesn't let you know it's really a jpeg image.
<html><body> <img src="/cgi-bin/imgscript.pl/bestimage.jpg"> </body></html>
#!/usr/local/bin/perl
# another example to spit out a jpeg file
$whichfile=$ENV{'PATH_INFO'};
print "Content-type: image/jpeg\n\n";
open FILE, $whichfile;
while (read FILE, $f, 16384) {
print $f;
}
close FILE;
Now, if this file is saved, the name "bestimage.jpg" will be used,
because /cgi-bin/imgscript.pl/bestname.jpg looks like the
complete path and filename of bestname.jpg image. <html><body> <img src="/cgi-bin/imgscript.pl/bestimage.jpg?big"> </body></html>Here's the Perl script that takes the size parameter and uses it to modify the filename:
#!/usr/local/bin/perl
# an example of spitting out a jpeg file
# using both PATH_INFO and QUERY_STRING
$whichfile=$ENV{'QUERY_STRING'}."-".
$ENV{'PATH_INFO'};
print "Content-type: image/jpeg\n\n";
open FILE, $whichfile;
while (read FILE, $f, 16384) {
print $f;
}
close FILE;
Now, this will look for the file named "big-bestimage.jpg"
in the current directory.
Author: Doug Steinwand
Date: [09/23/97]
More articles about CGI
More articles by Doug Steinwand
Author Biography