Solved

Perl cannot print contents of document to Web browser

Posted on 2007-04-08
9
224 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

762 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

23 Experts available now in Live!

Get 1:1 Help Now