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
Chichester, United Kingdom | West Sussex County Council
  Testing Manager, Chichester, £42,222 - £45,090 (includes a Market Supplement, subject to review) The IT Testing function has until recently been performed within the project structure.  This new role recognises that we need to ... more >
London, United Kingdom | InterSystems
ARCHITECT / DEVELOPER, London, Very Competitive £  OBJECT ORIENTED DEVELOPER / PROGRAMMER / ARCHITECT with strong OO (object oriented) development experience required by world leading global software provider to act as Senior Technical Consultants. InterSystems Corporation ... more >
Chichester, United Kingdom | West Sussex County Counci
Application Specialist, Chichester, £26,400 - £28,600 pa (includes Market Rate Supplement) IT Services at WSCC supports and manages a variety of systems based on Oracle databases that include third party and bespoke applications as well ... more >
Sutton, Surrey, United Kingdom | Royal Marsden Hospital NHS Trust
  The Royal Marsden NHS Foundation Trust is a centre of excellence for research, development, education and care in the treatment of cancer. Analyst Programmers, Band 6, £23,458-£31,779 plus 15% HCAS, Sutton, Surrey We are ... more >
More job opportunities