You may use any UNIX environment variable within a Perl script. To use an environment variable, use a string of characters like $ENV{'variable'} where you replace the word variable with the name of the environment variable of interest. For example, to use the REMOTE_HOST environment variable within an Perl script, use the string $ENV{'REMOTE_HOST'}. The following fragment prints the value of the Present Working Directory environment variable, PWD:
#!/usr/local/bin/perl print "Content-type:text/html\n\n"; print <<EOM; The current working directory is $ENV{'PWD'} EOMThe following environment variables are established by the Web server, the Perl interpreter, and/or CGIWrap and may be used in Perl scripts, followed by some sample values:
#!/usr/local/bin/perl # open(COUNTER, "+< /home/smith/counter.file"); # open the counter file with read and write access. $COUNT= <COUNTER>; #read the current value. $COUNT++; #increment it by one. seek(COUNTER, 0 , 0); #rewind the file. print COUNTER $COUNT; #write the new value to the file. close COUNTER;The line $COUNT++; adds one to the value of the variable $COUNT.
To include the number from the count file in a document, use the print command as before. For example, you might build a Perl script like the following:
#!/usr/local/bin/perl print "Content-type:text/html\n\n"; # #First, increment the counter: # open(COUNTER, "+< /home/smith/counter.file"); # open the counter file with read and write access. $COUNT= <COUNTER>; #read the current value. $COUNT++; #increment it by one. seek(COUNTER, 0 , 0); #rewind the file. print COUNTER $COUNT; #write the new value to the file. close COUNTER; # #Then use the counter in the returned HMTL. # print <<END_OF_PAGE; <html> <title>My first return page</title> <h1>Thank you for selecting this document.</h1> You are visitor number $COUNT </html> END_OF_PAGEThis script reads the counter file into a variable called COUNT, increments the counter variable by one, stores the new value in the counter file, and then returns an HTML document that uses the counter variable.
However, Perl does include facilities for synchronizing file accesses. Among others, the flock function allows users to lock a file for private use. You could use the line
flock (COUNTER, 2);to lock the file immediately after opening the file and the line
flock (COUNTER, 8);to unlock the file immediately before closing it.
This problem applies to other Perl building blocks and to scripts that simultaneously write to, or simultaneously write to and read from the same file, written in any language . In general, the problem can be controlled by giving any writing script private access. That is, if a script is writing, no other writers or readers will be given access until that script is finished. The Perl flock function grants such privacy.
On the other hand, any solution will have drawbacks. For example, it may be inconvenient to lock the file for a long period of time, in which case other approaches may be needed. The values you can pass to flock are
open (MAIL, "| /usr/lib/sendmail -oi -n -t" ); print MAIL <<MAIL_MESSAGE; To:username\@www.cc.ukans.edu From:username\@www.cc.ukans.edu You have just sent info about vegetarianism to another interested person. MAIL_MESSAGE close MAIL;The information in the To: and From: clauses can be any e-mail address. Reply-to: and Return: clauses can be appended to identify to whom any return message should be sent. These are not always the same as the From: address. You can also include a Subject: clause.
After these lines, you may add any text you want, including field names embedded within $in{...} strings. You might construct the following fragment to mail information to the requestor:
require ("/usr/local/lib/perl5/cgi-lib.pl"); &ReadParse; open (MAIL, "| /usr/lib/sendmail -oi -n -t" ); print MAIL <<MESSAGE_TO_USER; To:$in{'address'} From:smith\@www.cc.ukans.edu Dear $in{'name'}: Here are some books that talk about vegetarianism: Diet for a New America How to Avoid Beef MESSAGE_TO_USER close MAIL;In this example, "name" and "address" are names of fields specified in the example form, and they would be replaced by the user's name and e-mail address in the actual e-mail message sent to her. In this example, both the mail header information and the enclosed mail message contain references to fieldnames.