PerlHoo, Part I | 5
PerlHoo::User Interface
As mentioned previously, PerlHoo knows which resource category to display based on the PATH_INFO environment variable, which is added to the end of the base URL defined by $baseurl
in Line 21.
So for example, if $baseurl
is https://www.webreference.com/cgi-bin/perl/perlhoo.pl and we wanted to display the Computers/Internet category, the URL would be https://www.webreference.com/cgi-bin/perl/perlhoo.pl/Computers/Internet. One of the primary benefits of this scheme is that it allows us to arbitrarily link to specific categories.
Since most of the code deals with displaying the HTML interface, let's examine the code in more detail. If you haven't browsed it yet, you may want to take a look at the source for reference. The source is organized into 4 sections:
- Loading modules (Lines 14-16) - We load the strict pragma, and CGI and Text::CSV modules
- Global variables (Lines 19-21) - Here we define the global variables:
$datafile
- the name of the csv file that contains the Web directory data. A separate file will exist in each directory$rootdir
- the local path to the top level resource directory$baseurl
- the base URL of PerlHoo (i.e. https://www.webreference.com/cgi-bin/perl/perlhoo.pl)
Main Body
The main code body is relatively simple in that it only includes three basic steps. The first step (Lines 24-25) creates a new instance of the CGI class and prints the HTTP header. The second step (Lines 27-29) grabs the value of the PATH_INFO environment variable, assigns it to the$reldir
variable and strips any /
characters from the beginning and end of the string. The third step (Lines 31-33) executes the 3 subroutines that print the PerlHoo header, sub-categories, and links to resources in the category.
Subroutines
For the sake of clarity, let's briefly explore the logic and syntax of each of the major subroutines:- print_header (Lines 70-94) - this subroutine prints the PerlHoo headers including a set of links delimited by a
:
character that identifies the current category and its parents. To create links to the parent categories, we must first split the category path (Line 72), which was passed in as an argument. In Lines 74-79 we print basic HTML. Lines 82-92 iterate through each directory starting from the top level directory building links to each subsequent sub-directory until it reaches the current directory. - print_categories (Lines 42-68) - this subroutine prints the sub-categories of the current category. The relative directory is passed in from the PATH_INFO environment variable and combined with
$basedir
to determine the current working directory (Lines 43-45). Next, we get a list of sub-directories (Lines 47-49). In lines 51-66, we loop over each sub-directory, displaying a link to each. - print_links (Lines 96-108) - this subroutine opens a specified CSV file (Line 99), parses each line using the
fields
method of the Text::CSV module (Lines 103-104), and prints each entry (Line 105).
Produced by Jonathan
Eisenzopf and
Created: Mar. 18, 1999
Revised: Mar. 22, 1999
URL: https://www.webreference.com/perl/tutorial/2/