Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Script error when only 1 result returned



1:50 pm on May 5, 2009 (gmt 0)

5+ Year Member

A couple of years ago I paid for someone to develop a perl script that would query an xml feed to produce some html output and create a cache file for me to call.

I call the script using SSI and can build a query of the XML such as

<!--#INCLUDE VIRTUAL="/cgi-bin/xml.pl?"-->

<!--#INCLUDE VIRTUAL="/cgi-bin/xml.pl?keywords=florida"-->

Anything I add after the ? is appended to the xml url defined in $xmlurl in the script. This enables me to query the xml to return a list of relevant result. The results are produced in a table and for each result there is a table row.

This all works fine except when only 1 result is returned, no html prints out and no cache file is created.

When there are two or more results everything works fine. It has only come to my attention recently and the email address I had for the original developer is no longer valid.

I'd appreciate any assistance. My experience is with dreamweaver, photoshop, html etc and I have very limited knowledge of perl and have no idea what to change.

# Script for processing XML feed and producing XML output

use strict; # Because we should
use LWP::Simple; # for fetching XML over http
use XML::Simple; # for parsing XML

# config

my $cache = "/var/www/vhosts/domain/httpdocs/cache/"; # Path to cache directory
my $cachetime = "10"; # Cache time seconds
my $xmlurl = "http://www.example.com/cgi-bin/xml_search.exe?crypto_block=k---9xLZzqngfJwOM1OIWwuEWWxDt99EbVPD5-4_QRwaO_9sJI0EUjOZg4xxSUSSah6ZY&user_id=1234"; # Path to xml feed url
my $searchurl = "http://www.example.com.com/cgi-bin/search.exe?user_id=1234&s_buy=yes"; # path to search URL

$xmlurl .= $ENV{QUERY_STRING};

# Filename for cache

my $cachefile = $ENV{QUERY_STRING};
$cachefile =~ s/[^a-zA-Z0-9]//g;
$cachefile = $cache . $cachefile;

# Main

# Print content type header

print "Content-type: text/html\n\n";

# Check if cached already

if (-f $cachefile)
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($cachefile);
my $modified = time() - $mtime;
if ($modified < $cachetime)
open(CACHE, "$cachefile");
while (<CACHE>)


# Get XML

my $xmlfeed = get($xmlurl);

# Parse XML
my $parsexml = XMLin($xmlfeed);

# Get Events

my $events = %$parsexml->{event};

my $href;
my $event;
my $link;
my $table;

# Output Table Header
$table = <<'HEREDOC';
<table border="0" class="results" cellspacing="0" cellpadding="0" width="100%">

# for each hash in the array, produce a new table row.
for $href (@$events)
my $urlevent = url_encode($href->{event_desc});
my $urlprice = url_encode($href->{min_seat_price});
my $urlstart = url_encode($href->{start_date_desc});
my $urlend = url_encode($href->{end_date_desc});
my $urlcrypto = url_encode($href->{full_query}{crypto_block});
my $urlcrypto2 = url_encode($href->{event_info_button_url});

# - print each row

$table .= "<tr><td width=\"85%\"><b>$href->{event_desc}</b><br>$href->{venue_desc}, $href->{area_desc}<br>$href->{start_date_desc}<br>Prices from <b>&#163 $href->{min_seat_price}</b></td><td width=\"15%\"><a href=\"/cgi-bin/details.cgi?$urlcrypto2\">Full details</a><br><a href=\"\/cgi-bin/go.cgi?/web_perfs.exe/goto?&crypto_block=$urlcrypto\"> Buy</a></td></tr>\n";

# Output table footer
$table .= "</table>";

print $table;
# Output cache

print CACHE "<!-- from cache -->" . $table;


# Short routine for urlencoding
sub url_encode {
my $text = shift;
$text =~ s/([^a-z0-9_.!~*'( ) -])/sprintf "%%%02X", ord($1)/ei;
$text =~ tr/ /+/;
$text =~ s/http%3A//;
return $text;

[edited by: phranque at 9:18 pm (utc) on June 15, 2009]
[edit reason] disabled graphic smileys ;) [/edit]


3:12 am on Jul 14, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

why do you have the exit in the file check block?
if (-f $cachefile)


6:21 pm on Jul 15, 2009 (gmt 0)

5+ Year Member

This is probably the known issue of XML::Simple not returning a list when there is only one result. Try this:

my $parsexml = XMLin($xmlfeed, ForceArray => 1);

I don't know if that works using the simple API interface of XML::Simple but give it a try. If it doesn't your code needs to be rewritten using the OO interface of XML::Simple.


8:45 pm on Jul 17, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

welcome back, p_d!

Featured Threads

Hot Threads This Week

Hot Threads This Month