Solved

Perl cannot print contents of document to Web browser

Posted on 2007-04-08
9
228 Views
Last Modified: 2013-12-25
Perl 5 problem.  I just installed Perl 5.8.8 Build 20 on an new Windows 2003 server.  I am for some reason uanable to print the contents of a file to a web browser.  However, if I run the same script at the command line, I see all the correct output from the file there.  Why can I not get a handle on the any lines from the file and print them to the browser even using the below simple code.


#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "<HTML>\n";
open(FILES, "test.dat") or warn "no file exists: $!";
$record = <FILES>;
print "$record";
print "</HTML>\n";
0
Comment
Question by:NoodlesWIU
9 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18872630
check your error log file of the web server, most likely your web server cannot execute the file (probably due to lack of a known interpreter)
0
 
LVL 3

Author Comment

by:NoodlesWIU
ID: 18872697
Actually I was able to partially resolve it.  For some reason I have to type in the absolute path of the file as apposed to the relative path.  ie.)  my script is in a sub folder of the root say folder 1, I should be able to use this script because the file is relative to where the script is at.

open(FILES, "test.dat") or warn "no file exists: $!";
$record = <FILES>;
print "$record";

instead I found that I have to do this...

open(FILES, "./folder1/test.dat") or warn "no file exists: $!";
$record = <FILES>;
print "$record";

Why do I have to type the absolute path instead of the relative path where the actual script file is at?  I would like to change this if possible
0
 
LVL 84

Expert Comment

by:ozo
ID: 18873321
Actually a path starting with . is a relative path.
you can
use Cwd;
print getcwd;
to show the current working directory.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 10

Expert Comment

by:xanius
ID: 18873491
Well.

acoordiung to your (partlyx successfule code),

#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "<HTML>\n";
open(FILES, "folder1/test.dat") or warn "no file exists: $!";
$record = <FILES>;
print "$record";
print "</HTML>\n";

would work ....
0
 
LVL 84

Expert Comment

by:ozo
ID: 18873503
the current working directory is not necessarily where the script is,
is is usually the current working directory inherited from the parent that invoked the script
0
 
LVL 84

Expert Comment

by:ozo
ID: 18873734
if you want to set the current working directory to the directory that the script is in, you might try
use File::Basename;
chdir dirname($0);
or
chdir dirname(__FILE__);
0
 
LVL 3

Author Comment

by:NoodlesWIU
ID: 18873795
Interesting.  In all of my previous experience, the working directory was always the directory where the original file was located.  Example being in HTML coding with regards to images.  if the html file was in the same directory as the image, I would always use <img src="./image_name.ext">  I am still used to Microsoft products however am delving deeper into linux using Asterisk for my PBX system, So I am still somewhat of a novice in regards to linux enviornments.

So how does the working directory change?
0
 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 150 total points
ID: 18873804
The working directory of the web server (which is executing the script when it fails) is different than what you're using at the command line (where the script works).
0
 
LVL 84

Accepted Solution

by:
ozo earned 350 total points
ID: 18874063
Whoever calls the script can set the working directory that it starts with.
the current working directory has nothing to do with the base href used in img src links.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

828 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