image: search box screenshot
With a search box added we have a page that fits the design of our website and works in a way most users will understand
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: Simple templates with Perl

How to create a professional-looking, user-friendly web-based search function in Perl

Nigel Whitfield, Personal Computer World 07 Mar 2007
Download for this article
ADVERTISEMENT

We recently looked at the basics of Perl and saw how a very simple script could be used to extract information from a file on a web server URL.

We showed how saving a file in Csv format from Excel and writing a few lines of script would make it accessible from any web browser and support basic queries.

The results were displayed as text, which may not be enough if you want to create a good-looking site.

This month, we’ll look at some more functions in Perl and show you how to use pattern matching and substitution to create a script that does much the same job, but uses an HTML file as a template to create attractive results.

Making a template
The first step is to design the page for your results. Some web design tools, such as Dreamweaver, have methods of creating templates for database-driven websites, but here we just want something simple.

Using whatever method you fancy, mock up a page designed to show someone’s name, mobile and home phone numbers as in my test script (see attached pdf). You can add extra information if you wish.

We’ll use pattern matching to replace information in the template with data from our Excel Csv file. Start by picking as examples some data that should fit – for example, the longest name in the database – and create a page that looks good.

Try to ensure there’s a clearly defined area of the page where the information will appear and that your design would look good if the contact details were copied and repeated immediately below where they appear. This is so that things still look good when you have more than one result.

Ideally, you want a page that has information above and below the results, such as the company logo, links to other parts of the site and so on.

Next, you need to turn the page into a template that our Perl script can work with. Replace the dummy data with something that doesn’t appear anywhere else in the page. For example, the sequence --NAME-- probably won’t be found elsewhere, and similarly --HOME-- and

--MOBILE-- can be used for the home and mobile phone numbers. We’ll put code in the script to replace these tokens with the genuine data.

How does the script know which is the part of the page it can repeat if there are multiple entries that match our search term? We’ll add a bit of code that marks the start and end of the area where the contact details for each person appear. Do this using HTML comments, so there’s nothing visible on the page. If the editor you use doesn’t have an option to insert comments, switch to viewing the source and enter a line that says

<!-- TEMPLATE BEGIN -->

above the start of the contact area, and below it, add one that says

<!-- TEMPLATE END -->

Take care to avoid breaking any tags, or causing problems with nested tags, if the area is repeated.


All Online
Tags: Web Development

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
(Poole, Bournemouth, Dorset, Hampshire), United Kingdom | RNLI
Analyst - Network & Telecoms - £35,000+ - Poole, Bournemouth, Dorset, Hampshire Our data and voice network team's impact on the organisation is considerable. And with something in the region of 5,000 direct users connected ... more >
Telford, Shropshire, United Kingdom | EDS
EDS are currently looking to recruit a PMO Support Analyst to join our Project Management Defence team in Telford, Shropshire. Summary: Within DII Service Management. To perform the PMO function for SM Service Introduction. This ... more >
Sandiacre, Nottinghamshire, United Kingdom | NHS Midlands
Workstream Lead Requirement, Design, Build and Test (Business Analyst) Strategic IM&T - Delivery   Band 7:      £29,091 - £38,352 per annum Hours:       37.5 per week Base:         Octavia House, Sandiacre Job Ref:     973 - 080810   ... more >
Inverness, United Kingdom | NHS Scotland
CORPORATE SERVICES E-HEALTH DEPARTMENT  RAIGMORE HOSPITAL INVERNESS TECHNICAL DEVELOPMENT TEAM IT TECHNICAL SPECIALIST  £24,103 to £32,653 PA An exciting opportunity has arisen to join the technical development team within the eHealth Department. We are looking ... more >
More job opportunities