Solved

Perl errors: "seek() on unopened filehandle", "readline() on unopened filehandle", and "XML::Mini Error MESSAGE:XML::Mini::Document::fromFile() Can't find file"

Posted on 2013-05-17
4
890 Views
Last Modified: 2013-12-25
I usually have run my CGI script on Debian without problems. I tried to install it on a fresh install of Ubuntu, but receive this in the Apache 2 error.log:

[Fri May 17 13:06:09 2013] [client 192.168.1.108] seek() on unopened filehandle at /usr/lib/cgi-bin/NFS1.pl line 432.
[Fri May 17 13:06:09 2013] [client 192.168.1.108] readline() on unopened filehandle at /usr/lib/cgi-bin/NFS1.pl line 433.
[Fri May 17 13:06:09 2013] [client 192.168.1.108] Use of uninitialized value $input in pattern match (m//) at /usr/local/share/perl/5.14.2/XML/Mini/Document.pm line 271.
[Fri May 17 13:06:09 2013] [client 192.168.1.108] Use of uninitialized value $filename in -e at /usr/local/share/perl/5.14.2/XML/Mini/Document.pm line 246.
[Fri May 17 13:06:09 2013] [client 192.168.1.108] Use of uninitialized value $filename in concatenation (.) or string at /usr/local/share/perl/5.14.2/XML/Mini/Document.pm line 246.
[Fri May 17 13:06:09 2013] [client 192.168.1.108] XML::Mini Error MESSAGE:XML::Mini::Document::fromFile() Can't find file
[Fri May 17 13:06:09 2013] [client 192.168.1.108] [Fri May 17 13:06:09 2013] NFS1.pl: XML::Mini Error MESSAGE:XML::Mini::Document::fromFile() Can't find file
[Fri May 17 13:06:10 2013] [client 192.168.1.108] File does not exist: /var/www/favicon.icon


The browser gives this output:
Software error: XML::Mini Error MESSAGE:XML::Mini::Document::fromFile() Can't find file

Here are the code blocks that the error code refers to in my script:
seek() on unopened filehandle at /usr/lib/cgi-bin/NFS1.pl line 432
readline() on unopened filehandle at /usr/lib/cgi-bin/NFS1.pl line 433.

These lines are lines 31 and 32 below:
sub getMovieInfo {
	my $movieName = shift;
	my $processedMovieName = nameProcessor($movieName);
	my $searching = 1;
	my ($imageLink, $rating, $genre, $imdburl, $plot, $year);
	#This loop will run maximum twice. Again if the moviename ends in a four digit number and we didn't find anything the first time.
	#The second time it runs it will be without the four digit number at the end of the movie name.
	while($searching) {
		my $xmlDoc = XML::Mini::Document->new();
		#1. Thus URL will give us an XML document to parse:
		my $url = "http://imdbapi.org/?title=".uri_escape($processedMovieName)."&type=xml&plot=simple&episode=1&limit=1&yg=0&mt=none&lang=en-US&offset=&aka=simple&release=simple&business=0&tech=0";
		#print "<b>".$processedMovieName."</b> - Trying: <font size=\"-1\">" . $url . "</font><br>";
		#2. Download the url and extract the information(imageLink/cover: OK, genre: OK, rating: OK):
		my $xml;
		$genre = " ";
		#print "Called getMovieInfo() for " . $movieName . "<br> - " . $processedMovieName . "<br> - ".removeNoiseWords(lc($movieName))."<br><br>";
		if($url =~ /http/) {
			#print "   URL OK!<br>";
			my $response_body = tempfile();
			my $curl = WWW::Curl::Easy->new;

			$curl->setopt(CURLOPT_HEADER, 1);
			$curl->setopt(CURLOPT_URL, $url);
			$curl->setopt(CURLOPT_WRITEDATA, \$response_body);

			my $return_code = $curl->perform;
		
			my $getNextLine = 0;
			if ($return_code == 0) {
				my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
				seek($response_body, 0, SEEK_SET);     # reset filehandle to beginning of file
				while (<$response_body>) {
					$xml .= $_;
				}
				$xmlDoc->parse($xml);
				my $xmlHash = $xmlDoc->toHash();
				$rating = $xmlHash->{'IMDBDocumentList'}->{'item'}->{'rating'};
				if(ref $xmlHash->{'IMDBDocumentList'}->{'item'}->{'genres'}->{'item'} eq 'ARRAY') {
					foreach my $genreElement (@{$xmlHash->{'IMDBDocumentList'}->{'item'}->{'genres'}->{'item'}}) {
						$genre .= $genreElement . " ";
					}
				} else {
					$genre .= $xmlHash->{'IMDBDocumentList'}->{'item'}->{'genres'}->{'item'} if(exists($xmlHash->{'IMDBDocumentList'}->{'item'}->{'genres'}->{'item'}));
				}
				if(ref $xmlHash->{'IMDBDocumentList'}->{'item'}->{'poster'} eq 'ARRAY') {
					foreach my $imageLinkElement (@{$xmlHash->{'IMDBDocumentList'}->{'item'}->{'poster'}}) {
						$imageLink .= $imageLinkElement . " ";
					}
				} else {
					$imageLink = $xmlHash->{'IMDBDocumentList'}->{'item'}->{'poster'};
				}
				$imdburl = $xmlHash->{'IMDBDocumentList'}->{'item'}->{'imdb_url'};
				$plot = $xmlHash->{'IMDBDocumentList'}->{'item'}->{'plot_simple'};
				$year = $xmlHash->{'IMDBDocumentList'}->{'item'}->{'year'};
			} elsif($return_code == 56) {
				#If the return code is 56, then we have been interrupted. We loop and try again.
				$searching = 1;
				next;
			} else {
				print ("An error occured: ".$return_code." ".$curl->strerror($return_code)." ".$curl->errbuf." at initiateMovieInfo() when treating ". $url ."\n");
			}
			#print "   -->" . $rating . " " . $imageLink . " " . $genre . "<br>";
		}
		if(defined($rating)) {undef($rating) if($rating eq "");}
		if($processedMovieName !~ m/\b\d{4}\b$/){$searching = 0;}
		if($processedMovieName =~ m/\b\d{4}\b$/ && !defined($rating)){$processedMovieName =~ s/\b\d{4}\b$//;}
	}
	$imageLink =~ s/\s+$// if(defined($imageLink)); #remove trailing spaces
	if(defined($genre)) { $genre =~ s/\s+$//; $genre =~ s/^\s+//;} #remove trailing spaces #remove leading spaces
	#print "   Returning -->" . $rating . " " . $imageLink . " " . $genre . "<br>";
	my @infoArray = ($imageLink, $genre, $rating, $imdburl, $plot, $year);
	return \@infoArray;
}

Open in new window


All help is very much appreciated.
0
Comment
Question by:itnifl
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39174391
can you show the code for  tempfile()
0
 
LVL 2

Author Comment

by:itnifl
ID: 39174589
I have this in the start of the script:
use File::Temp qw/tempfile/;

I wasn't sure if I got that module correctly. I installed libfile-temp-perl, but I am not sure that is correct.

I also tried this:
perl -MCPAN -e "CPAN::Shell->force(qw(install File::Temp));"

It says:
All tests successful.
Files=12, Tests=131, 14 wallclock secs ( 0.10 usr  0.06 sys +  0.59 cusr  0.16 csys =  0.91 CPU)
Result: PASS
  DAGOLDEN/File-Temp-0.2301.tar.gz
  make test -- OK
Running make install
Manifying blib/man3/File::Temp.3pm
Installing /usr/local/share/perl/5.14.2/File/Temp.pm
Installing /usr/local/man/man3/File::Temp.3pm
Appending installation info to /usr/local/lib/perl/5.14.2/perllocal.pod
  DAGOLDEN/File-Temp-0.2301.tar.gz
  make install  -- OK


However, still same problem.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39174711
did you mean to say
  $curl->setopt(CURLOPT_WRITEDATA, $response_body);
0
 
LVL 2

Author Closing Comment

by:itnifl
ID: 39174967
Yes, that fixed the issue, but obviously my script does not work like it did on Debian. There are a bunch of problems i need to fix to make it work on Ubuntu. i guess there are some version differences :/
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now