10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

46CHAPTER 2 ■ POPULAR CGI MODULESmy $useragent = $ENV{'HTTP_USER_AGENT'};Next is the <strong>Perl</strong> conditional if-elsif-else <strong>to</strong> take an action based on a word found withinthe $useragent variable that is unique <strong>to</strong> the browser:if ($useragent =~ /Firefox/) {print p("You are visiting with a Firefox browser");} elsif ($useragent =~ /MSIE/) {print p("You are visiting with an Internet Explorer browser");} else {print p("Could not determine browser: $useragent");}Notice that if the browser cannot determine the browser, the default action is <strong>to</strong> dosomething sane <strong>to</strong> handle the problem—as opposed <strong>to</strong> producing an error or, worse yet,crashing, when this variable isn’t present or is something unexpected.The result when viewing this program through a web browser is shown in Figure 2-5.Figure 2-5. Using the user agent <strong>to</strong> determine the browser

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!