CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
"Unknown error"

 



perlfarmer
User

Aug 8, 2011, 12:11 AM

Post #1 of 3 (593 views)
"Unknown error" Can't Post

Hi,

I have written a perl script for a web page, get_text.pl, in directory dir that requires Analyze.pm (on line 6) in dir/cg-bin. No matter how I specify the path name for the "require" directive I get:


Code
Unknown error 
Compilation failed in require at get_text.pl line 6.


Here are the first 6 lines:


Code
#!/usr/bin/perl 

use strict;
use CGI::Carp qw(fatalsToBrowser);

require "Analyze.pm";


The only thing I can think of is that there is a problem with the .pm file. Here it is:


Code
use strict; 
use warnings;
use GD::Graph;

sub analyze
{
#my $k = shift(@_);

my $input = "";
open (INPUT, "<", "text.txt") or &error("System Error: Could not open internal file for reading.");
while (<INPUT>)
{
$input .= $_;
}
# Strip tags.
$input =~ s/<.*>//m;
# Replace all whitespace with a single space.
$input =~ s/\s/ /m;
# Get an array of all the words.
my @words = split(/ /, $input);
# Hash the words by counting their occurrences.
my %wordsHash = ();
foreach (@words)
{
# Strip any punctuation that came with the word.
$_ =~ s/[\.\?:;!,\(\)\-\*\[\]]*//;
if (wordsHash{$_})
{
$wordsHash{$_}++;
}
else
{
$wordsHash{$_} = 1;
}
}
my %charsHash = ();
foreach (@words)
{
my @chars = split(//, $_);
foreach (@chars)
{
if ($_ =~ /[A-Za-z]/)
{
if ($charsHash{$_})
{
$charsHash{$_}++;
}
else
{
$charsHash{$_} = 1;
}
}
}
}

my $numSentences = 0;
foreach (@words)
{
$_ =~ s/[\(\)]*//;
if ($_ =~ /\w*[\.\?!]*/)
{
$numSentences++;
}
}

my $numWords = scalar(@words);

my $numUniqueWords = keys(%wordsHash);

my $numEnglishCharacters = 0;
my $key;
my $value;
while (($key, $value) = each %charsHash)
{
if ($key =~ /[A-Za-z]/)
{
$numEnglishCharacters += $value;
}
}

my %freqHash = ();
while (($key, $value) = each %charsHash)
{
if ($key =~ /[A-Za-z]/)
{
$freqHash{$key} = $value/$numEnglishCharacters;
}
}

my @numData = (["Sentences", "Words", "Unique Words", "English Characters"], ["$numSentences", "$numWords", "$numUniqueWords", "$numEnglishCharacters"]);
my $mygraph = GD::Graph::bars->new(500, 300);
my $myimage = $mygraph->plot(\@numData) or &error("System Error: Could not plot graph");

print "Content-type: text/html\n\r\n\r";
print "<html><head><body>\n";
print "<h2>Hello World!!!</h2>";
print "</body></head></html>\n";
}

sub error
{
my $message = shift(@_);
print "Content-type: text/html\n\r\n\r";
print "<html><head><body>\n";
print "<div align=\"center\"><h2>Error</h2></div>";
print "<div align=\"center\">$message</div>";
print "<br><br>";
print "<div align=\"center\"><form>";
print "<input type=\"button\" value=\"Back\" onclick=\"window.location.href='http://www.kevin.thegreenage.org/public_html/index.html'\">";
print "</form></div>";
#print "<div align=\"center\"><a href=\"http://www.kevin.thegreenage.org/public_html/index.html\">Back to Main Page</a><div>";
print "</body></head></html>\n";
}

1;


Can anyone see a reason why this .pm would cause the "unknown error" and cause the compilation to fail?

Any advice is appreciated.

Regards.


(This post was edited by perlfarmer on Aug 8, 2011, 12:55 AM)


perlfarmer
User

Aug 8, 2011, 12:58 AM

Post #2 of 3 (585 views)
Re: [perlfarmer] "Unknown error" [In reply to] Can't Post

I believe I found the error in the preceding file. However I am getting a persistent "Premature end of script headers" error from the following files:


Code
#!/usr/bin/perl 

use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);

require "Analyze.pm";

my $Text;
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $Text, $ENV{'CONTENT_LENGTH'});
}

my $label;
my $input;
($label, $input) = split(/=/, $Text);
$input =~ tr/+/ /;

if (!$input)
{
print "Location: http://www.kevin.thegreenage.org/public_html/index.html\n\n";
}

my $length = length($input);
if ($length > 5120000)
{
&error("Text area exceeds maximum size of 5120000 characters.\n");
exit;
}

open (FILE, ">", "/home/kfarmer/public_html/kevin/text.txt");
print FILE $input;
close FILE;

&analyze;
exit;



Code
sub analyze 
{
print "Content-type: text/html\n\r\n\r";
print "<html><head><body>\n";
print "<h2>Hello World!!!</h2>";
print "</body></head></html>\n";
}

1;


I am truly stumped by this and I really hope someone can spot the error.

Any advice is appreciated.

Regards.


FishMonger
Veteran / Moderator

Aug 8, 2011, 8:08 AM

Post #3 of 3 (579 views)
Re: [perlfarmer] "Unknown error" [In reply to] Can't Post

First, check the file permissions to make sure they are correct.

Don't use quotes in the require statement when loading a module.

If the directory where the module is located is not in the @INC path, then include the directory in the script via a 'use lib' statement.


Code
use lib '/some/path'; 
use Analyze;


Having the "module" output the html directly is a poor design and usage of a module. It should return the data and the main script should handle the html output. IMO, putting those 2 subs in a separate "module" makes no sense, but if you're going to do it, write a proper module that exports the subs.

The method you're using to read/parse the POST submission has been depreciated for at least 10 years. Use the CGI module!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives