Open Source Web Development with LAMP Using Linux, Apache, MySQL, Perl, and PHP [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Open Source Web Development with LAMP Using Linux, Apache, MySQL, Perl, and PHP [Electronic resources] - نسخه متنی

James Lee, Brent Ware

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










7.6 CGI.pm HTML Shortcuts


In addition to the methods discussed previously, CGI.pm has functions for most of the commonly used HTML tags, including headings (h1(), h2(), h3(), etc.), paragraph breaks (p()), lists (li(), dl(), ul(), dd(), etc.), and text-formatting commands (i(), em(), blockquote()). See the CGI.pm documentation for details of all the shortcuts available.


7.6.1 Using HTML Shortcuts


Using these shortcuts is as straightforward as the previous example. Printing a heading:


<H1>Welcome to www.opensourcewebbook.com</H1>

becomes:


$server_name = `/bin/hostname`;
print h1("Welcome to $server_name")

An HTML paragraph, <P>, is formatted as print p(); (no arguments), while


print p(´This is a new paragraph´);

is equivalent to this:


<P>This is a new paragraph</P>

A list of arguments gets concatenated. This CGI.pm code:


$name = ´John Doe´;
print p(´hello ´, $name, ´, how are you?´);

results in this:


<P>hello John Doe, how are you?</P>

Like HTML commands, the shortcuts can be nested inside one another:


<P>Here is some <B>bold</B> text</P>

The previous statement can be programmed in CGI.pm as


print p(´Here is some ´, b(´bold´), ´ text´);

We can use one example to show many of these methods. The following program uses these shortcuts and others as well as a Perl built-in function (localtime()) and a CGI.pm method (server_name()). In the end, with all this fancy Perl programming, this program creates the same web page created in the earlier example (see Figure 7.5) but in a much more flexible way:


#! /usr/bin/perl
# info3.cgi
use strict;
use CGI´:standard´;
my $host = server_name();
my $date = localtime();
print
header(),
start_html(-title => ´System Information´,
-bgcolor => ´#520063´,
-text => ´#ffffff´),
h1("Hello from $host!"),
"The current time is now: $date",
end_html();

After CGI.pm is used, the program gets the local time and the hostname, as in the earlier example. The server_name() function, provided by CGI.pm, returns the name of the webserver. The localtime() function is a Perl built-in function that determines the local time on the machine; when assigned to a scalar, here $date, it returns the time in a nice, readable format. After these dynamic values are obtained, the HTML is printed, using the just-obtained values of $host and $date.


7.6.2 Named Parameters versus Ordered Arguments


When we executed start_html(), we used named parametersfor example, -title and -bgcolor. CGI.pm provides this way of invoking functions so that we can pass a lot of data into them in a readable, maintainable way. But there is another way of calling CGI.pm functions, which is based on the order of arguments.

For instance, we could invoke start_html() as:


print start_html(-title => ´My Title´);

or by order, as in:


print start_html(´My Title´);

CGI.pm is written so that if named parameters are not used, it knows the order of the arguments. We know that the first argument to start_html() is the title. This invites the question, What is the second argument? This difficult-to-remember detail is obtainable by quickly skimming the documents (perldoc CGI).


/ 136