Perl Basics   «Prev  Next»

Lesson 5Perl is easy
Objective Discover how Perl's flexible syntax and powerful library make it easy

Perl is an Easy Language

Perl programs are basically easy to write. The reason lies in Perl's flexible syntax and library.
This quick example shows a program that decides what browser is being caled by a web page and returns the correct HTML file for it:
#!/usr/bin/perl
print "content-type: text/html\r\n\r\n";
$ua = $ENV{'HTTP_USER_AGENT'};
($browser, $version) = split("/", $ua, 2);
($version, $extra) = split("[/\( ]", $version, 2);
if(($browser eq "Mozilla") && ($version =~ /^4.*/)) {
	printfile("moz4.html"); 
}
elsif(($browser eq "Mozilla") && ($version =~ /^3.*/)) {
	printfile("moz3.html"); 
}
elsif(($browser eq "Mozilla") && ($version =~ /^2.*/)){
	printfile("moz2.html"); 
}
else{
	printfile("aol.html"); 
}

sub printfile{
	$filename = shift;
	open(WEBFILE, "<$filename") || 
	print("cannot open $filename<br>\n");
	while (<WEBFILE>) { 
		print 
	}
	close WEBFILE;
}


Browser Name and Version Numbers

Notice how simple it is to break out the browser name and version numbers using split. Also, notice how easy it is to print the HTML file. To be able to print a whole file in one line of code, like while(<FILE>) { print } is tremendously useful, especially for Web programming. If you want to use this code, go ahead it's fully functional as it is. You will, of course, need to create the .html files for all the different browsers you intend to discriminate. That's just one example, and there are many more throughout this course. Once you know the language, Perl is remarkably easy for a language of its power. I am sometimes asked why I don't also check for Microsoft Internet Explorer. Here's my explanation.


Microsoft Internet Explorer

There are several browsers that pose as a Netscape browser [including Microsoft's Internet Explorer (MSIE)] by sending a user-agent string that says "Mozilla" with a valid Netscape version number. They do this to spoof programs that test the user-agent string, so they can receive Netscape-specific content. It is possible to test for these browsers, but I don't recommend it. The user-agent string is a part of the HTTP standard and was designed to identify the browser for many purposes, including rough content-negotiation (giving different content to different browsers). By spoofing the user-agent string, vendors like Microsoft are attempting to render it invalid.
If one browser wants to claim to be another, it becomes the responsibility of that browser to handle the content designed for the browser they are claiming to be. Of course, MSIE doesn't accept a lot of content designed for Netscape Navigator. In fact, some early versions of MSIE actually crash when given content like streaming server-push, and display broken picture icons when they encounter animated GIFs. These are the very reasons that the user-agent string was invented.
  • Detect Browser using Perl Regex: It is possible, in many cases, to actually detect these browsers. They may identify themselves with non-standardized strings in the comment portion of the user-agent field or in some other field. You can often use Perl's regular-expression matching to detect those browsers like in this MSIE example:
    elsif(($browser eq "Mozilla") && ($version =~ /^2.*/)){
    if($extra =~ /MSIE/) { 
    	printfile("msie3.html"); 
    }
    else{ 
    	printfile("moz2.html"); 
    }
    }
    

    Detecting different versions of MSIE is left as an exercise for you.

SEMrush Software