I have been battling this code for quite some time now and I can't for the life of me figure out why ALL sections work fine EXCEPT for if($p).
Everything prints proper headers and has proper output EXCEPT for if($p). For some reason this prints the site TWICE (the headers twice, that is). I can literally take out print $header within that section and it won't error out!! But when I do remove it, it prints one set of headers but then doesn't print out the sub routine INSIDE the page (it prints ABOVE the headers).
The only problem is where it's if($p) and that entire section. Any idea what's going wrong here?
Everything prints proper headers and has proper output EXCEPT for if($p). For some reason this prints the site TWICE (the headers twice, that is). I can literally take out print $header within that section and it won't error out!! But when I do remove it, it prints one set of headers but then doesn't print out the sub routine INSIDE the page (it prints ABOVE the headers).
The only problem is where it's if($p) and that entire section. Any idea what's going wrong here?
Code:
#!/usr/bin/perl
use warnings;
use strict;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
############################
# Configuration Section
############################
my $script = "[URL unfurl="true"]http://www.spyderscripts.com/cgi-bin/demo/30/lyrics.pl";[/URL]
## Change to the full URL of the lyrics file
my $cssloc = "[URL unfurl="true"]http://www.spyderscripts.com/demo/30/style.css";[/URL]
## Change to the full URL of the style.css file
my $title = "SpyderScripts- Free Perl and CGI archive and tutorials";
## Change to what you want the title of this page to be
my $bot = "SpyderScrypt's Bot";
## Create a name for your bot
############################
# Do not edit below this line
############################
######
# MY TEMPLATE STUFF
######
use LWP::Simple;
my $content = get("[URL unfurl="true"]http://www.spyderscripts.com/demo-template.shtml");[/URL]
my $breakline = "<!-- SCRIPT TEMPLATE BREAKS HERE -->";
$content =~ m|(.*)$breakline(.*)|s;
my $header = "$1";
my $footer = "$2";
#######
##################
# Hard coded values- DON'T TOUCH
##################
my $url = "[URL unfurl="true"]http://www.letssingit.com/frame_menu.html";[/URL]
use [URL unfurl="true"]WWW::Mechanize;[/URL]
my $mech = [URL unfurl="true"]WWW::Mechanize->new(agent=>"$bot");[/URL]
$mech->get($url);
print header, start_html(-title=>"$title", -style=>{'src'=>"$cssloc"});
if (!param()) { print $footer; }
################################
# URL page results
################################
my $p = url_param("p");
if ($p)
{
print $header;
use LWP::Simple;
my $page = "[URL unfurl="true"]http://www.letssingit.com/$p";[/URL]
my $newcontent = get($page);
#########
# Before we save over our HTML in $content we need to take the song title of the page
#########
#$newcontent =~ m[<H1 style="font:bold 18px verdana;margin-bottom:5px">([^<]+)</H1>];
$newcontent =~ m[<H1 style="font:bold 18px verdana;margin-bottom:5px">([^<]+)</H1>];
my $lyrics_title = $1;
$newcontent =~ m/< # Start of "pre" tag
(?:pre|PRE) # word "pre" in either case
[^>]*> # all attributes and then end of tag ">"
( # Capture
[^<]+ # Any non tag text
) # End capture
<\/(?:pre|PRE)> # Trailing "pre" tag in either case
/xs;
$newcontent = $1;
$newcontent =~ s/\n/<br>/g;
#########
# Print the lyrics result page
#########
print <<" ENDTHIS";
<center>
<p>
<p class="title"><font size="2">$lyrics_title</font></p>
<table class="lyrics">
<tr>
<td><p class="lyrics">$newcontent</p></td>
</tr>
</table>
</center>
ENDTHIS
print $footer;
exit;
}
###############
# Page Jumping if their search brought back too many results
###############
my $j = url_param("j");
if ($j)
{
print $header;
&search_form;
######
# Get rid of our hex %26 from our URL and put back our &s
######
$j =~ s/%26/&/g;
my $page = "[URL unfurl="true"]http://www.letssingit.com/cgi-exe/am.cgi?a=$j";[/URL]
######
# We need to capture the search query again
######
$j =~ m/s=(.+)&r=/;
my $query = $1;
###############
# Rip through the HTML again
###############
use LWP::Simple;
my @content = get($page);
my $content = join("", @content);
$content =~ s/self==top//;
########
# Pull back the number of pages of lyrics we have
########
$content =~ m|Page <B>\d+</B> of <B>(\d+)</B>|s;
my $page_count = $1;
$content =~ m|<TR><TD class=head>Artist</TD><TD class=head>Song</TD></TR>(.*)</TD></TR></TABLE></TD></TR></TABLE>|s;
my $listings = $1;
my @data = ();
while($listings =~ m|<TD class=\w><A href="([^"]+)">([^<]+)</A></TD><TD class=\w><A href="([^"]+)">([^<]+)</A></TD>|g)
{
push(@data, "$1:$2:$3:$4");
}
#####################
# Begin printing results table
#####################
print <<" END";
<center>
<table class="overflow">
<tr>
<td><div align="center"><b>Page Overflow: Jump to Page</b></div></td>
</tr>
<tr>
<td>
END
my $cnt = 0;
for (1 .. $page_count)
{
$cnt++;
print qq(<a href="$script?j=search%26l=song%26s=$query%26r=100%26m=%26p=$cnt">$cnt</a>, );
}
print "</td></tr></table></center>";
print <<" END";
<center>
<table class="results">
<tr>
<td colspan="2"><div align="center">Search Results for: <b>$query</b> </div></td>
</tr>
<tr>
<td><div align="center">ARTIST</div></td>
<td><div align="center">SONG</div></td>
</tr>
END
my $data_count = 0;
foreach my $key (@data)
{
$data_count++;
########
# Number is odd
########
if ($data_count%2)
{
my ($a, $b, $c, $d) = split (/:/, $key);
print <<" END";
<tr>
<td bgcolor="33FFCC"><div align="left">$b</div></td>
<td bgcolor="33FFCC"><div align="left"><a href="$script?p=$c">$d</a></div></td>
</tr>
END
}
#######
# Number is even
#######
else
{
my ($a, $b, $c, $d) = split (/:/, $key);
print <<" END";
<tr>
<td><div align="left">$b</div></td>
<td><div align="left"><a href="$script?p=$c">$d</a></div></td>
</tr>
END
}
}
print "</table></center>";
print $footer;
exit;
}
###############
# Submitted the search query
###############
if (param())
{
print $header;
&search_form;
my $query = param("query");
my $choice = param("choice");
$mech->submit_form(
form_name => 'search',
fields => {
s => $query,
l => "$choice"
},
);
########
# Check to see if the form was blank
########
if ($query eq "")
{
print qq(<p class="error">You must specify a search term.</p>);
print $footer;
exit;
}
##################
# SONG has been selected for search
##################
if ($choice eq "song")
{
#########
# Parse and store the HTML content
#########
my @content = $mech->content;
my $content = join("", @content);
$content =~ s/self==top//;
#################
# If the page displays Showing Results it means we need to print more information
# for the user to select which song they were looking for
#################
if ($content =~ /Showing results/)
{
$content =~ m|Page <B>\d+</B> of <B>(\d+)</B>|s;
my $page_count = $1;
$content =~ m|<TR><TD class=head>Artist</TD><TD class=head>Song</TD></TR>(.*)</TD></TR></TABLE></TD></TR></TABLE>|s;
my $listings = $1;
my @data = ();
while($listings =~ m|<TD class=\w><A href="([^"]+)">([^<]+)</A></TD><TD class=\w><A href="([^"]+)">([^<]+)</A></TD>|g)
{
push(@data, "$1:$2:$3:$4");
}
#################################################
# Begin printing results table
##################################################
#####################
# First, let's check to see if we need an overflow table
#####################
if ($page_count > 1)
{
print <<" END";
<center>
<table class="overflow">
<tr>
<td><div align="center"><b>Page Overflow: Jump to Page</b></div></td>
</tr>
<tr>
<td>
END
my $cnt = 0;
for (1 .. $page_count)
{
$cnt++;
print qq(<a href="$script?j=search%26l=song%26s=$query%26r=100%26m=%26p=$cnt">$cnt</a>, );
}
print "</td></tr></table></center>";
}
print <<" END";
<center>
<table class="results">
<tr>
<td colspan="2"><div align="center">Search Results for: <b>$query</b> </div></td>
</tr>
<tr>
<td><div align="center">ARTIST</div></td>
<td><div align="center">SONG</div></td>
</tr>
END
my $data_count = 0;
foreach my $key (@data)
{
$data_count++;
########
# Number is odd
########
if ($data_count%2)
{
my ($a, $b, $c, $d) = split (/:/, $key);
print <<" END";
<tr>
<td bgcolor="33FFCC"><p class="results">$b</p></td>
<td bgcolor="33FFCC"><p class="results"><a href="$script?p=$c">$d</a></p></td>
</tr>
END
}
#######
# Number is even
#######
else
{
my ($a, $b, $c, $d) = split (/:/, $key);
print <<" END";
<tr>
<td><p class="results">$b</p></td>
<td><p class="results"><a href="$script?p=$c">$d</a></p></td>
</tr>
END
}
}
print "</table></center>";
print $footer;
exit;
}
####################
# No search results were found
#####################
elsif ($content =~ /No search results/)
{
print qq(<p class="error">No matches found</p>);
print $footer;
exit;
}
else
{
#########
# Before we save over our HTML in $content we need to take the song title of the page
#########
$content =~ m|<H1 style="font:bold 18px verdana;margin-bottom:5px">([^<]+)</H1>|;
my $lyrics_title = $1;
#########
# Remove everything except the lyrics from the saved HTML content
#########
$content =~ m/< # Start of "pre" tag
(?:pre|PRE) # word "pre" in either case
[^>]*> # all attributes and then end of tag ">"
( # Capture
[^<]+ # Any non tag text
) # End capture
<\/(?:pre|PRE)> # Trailing "pre" tag in either case
/xs;
$content = $1;
$content =~ s/\n/<br>/g;
#########
# Print the lyrics result page
#########
print <<" END";
<center>
<p>
<p class="title">$lyrics_title</p>
<table class="lyrics">
<tr>
<td><p class="lyrics">$content</p></td>
</tr>
</table>
</center>
END
print $footer;
}
}
}
sub search_form
{
################################
# Print the search form
################################
print <<"ALL";
<center>
<table width="343" border="0">
<form action="$script" method="post">
<tr>
<td width="198"><font size="2">search</font>
<input name="query" type="text" id="query"></td>
<td width="74"><select name="choice" id="choice">
<option value="song" SELECTED>song</option>
</select></td>
<td width="57"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</form>
</table>
</center>
ALL
}