|
 |
 |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97
This code snippet introduces a new Perl function, join, which does the opposite of split. Once the numeric address is unpacked into four numbers in the @Addrs list, join puts them together into one string with the numbers separated by periods.
The last part of lookup.pl is straightforward. If the name is empty or the same as the string IP address from the command line, the address couldnt be found anywhere. Otherwise, the name and IP address are displayed.
TIP: How would you handle it if someone using lookup.pl entered no argument on the command line? Or too many? If youre thinking along those lines, beware: Youre starting to think like a programmer!
Making Improvements
If you find that its a tool you use a lot, you can make lookup.pl easier to run by putting it in a shell file on UNIX or a batch file on Windows 95 or NT. The UNIX file would contain this line: perl lookup.pl $1. In Windows, make it perl lookup.pl %1. If you call the file lookup on UNIX or lookup.bat in Windows, then you can just type lookup followed by the domain or IP you want to look up.
You can use what youve learned in this section to make improvements to your Web site, too. Recall from Skill 9 that the Windows Web servers put only IP addresses in their logs: Internet Information Server wont resolve addresses into names at all; the Sambar server will, but the developers recommend that you leave the capability turned off. Using what youve learned about the Perl networking functions, specifically gethostbyaddr, you can look up the names yourself.
You can also use the networking functions in your guest book and polling forms to get addresses and domain names from visitors, because the IP address of a connection is included in the CGI environment variables you can read from a Perl program. You might even consider changing the structure of the guest book data to include the visitors domain name.
Heres a caveat, however: Software that interrogates name servers, even fast, compiled software, runs relatively slowly because looking up names and Internet addresses is a time-consuming operation. Try to stay away from writing code that makes many repeated calls to gethostbyname or gethostbyaddr because you may bog your system down to a crawl. If you want to see how this works, try running lookup.pl with a domain name that you know is invalid and see how long it takes the script to come back with the Unable to resolve
message. |
EXERCISE: The Sambar Server: Building Your Web Site
You can use what youve learned in this skill about searching for information to add features to your Web site. You can also expand on the guest book concepts that you learned in Skill 7:
- Add two fields to the data structure for the guest book and use them to store the visitor's domain name and IP address. Do the address translation when the visitor enters the form, not as you display the list.
- Experiment with a database of employees, if you run a business, or an inventory database. Even maintaining a list of addresses and telephone numbers of friends will give you practice in manipulating database information.
Moving On
In Skill 13, youll delve into security on your Web sitehow to achieve it and maintain it.
Youll learn about general security issues, the specific problems and solutions of various systems, and how to keep intruders from entering or even damaging your system. Youll also see how you can use Perl to create encrypted passwords.
Are You Experienced?
Now you can…
- search for specific information in a structured database
- use multiple keys in your search
- understand how sockets came about in network programming
- use two Perl networking functions to look up domain names and IP addresses on the Internet
|