Solved

Perl cannot print contents of document to Web browser

Posted on 2007-04-08
9
225 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

930 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

10 Experts available now in Live!

Get 1:1 Help Now