cgi-bin script not printing output on html

V

Vikram

I am running Active Perl on windows XP, I have created a simple cgi-bin
script, which takes the input from a system command, lists all the
files in a directory and stores it in a array:

@array = split(/\n/, `dir . /B`);

for($i=0;$i<@array;$i++) {
print "<tr><td>$array[$i]</td></tr>";
}

I have the script printing each of the filename as output to a html,
strangely, none of the array elements gets printed out on the html
page, however they do if I run the script on the command line.
What is wrong here?

Thanks
Vikram
 
V

Vikram

I did define the $dir -
@array = split(/\n/, `dir $dir /B`); but the result is till the same,
it prints the output ok on command line but not on html page?
Thanks
Vikram
 
A

A. Sinan Unur

I am running Active Perl on windows XP, I have created a simple cgi-bin
script, which takes the input from a system command, lists all the
files in a directory and stores it in a array:

@array = split(/\n/, `dir . /B`);

for($i=0;$i<@array;$i++) {
print "<tr><td>$array[$i]</td></tr>";
}

Better:

print qq{<tr><td>$_</td></tr>} for (@array);

Is this the whole script? If it is, you are missing a lot of required
parts. If it is not, you are missing a lot of required parts.

Please read the posting guidelines for this group to see what you can do
to help others help you.
 
J

Jürgen Exner

Vikram said:
I am running Active Perl on windows XP, I have created a simple
cgi-bin script, which takes the input from a system command, lists
all the files in a directory and stores it in a array:

@array = split(/\n/, `dir . /B`);

for($i=0;$i<@array;$i++) {
print "<tr><td>$array[$i]</td></tr>";
}
[...]
What is wrong here?

Your coding style. It is way to C-like.
Much more perlish and much more readable would be:

print "<tr><td>$_</td></tr>" for (@array);

jue
 
V

Vikram

I am sorry not to have shown the entire script, however the script is
very simple, right now and is as follows:

#!c:/Perl/bin/perl.exe
#
# Use the following modules
#
use CGI qw:)standard);
use strict;
use warnings;
#
# Define variables and create an array
#
my
$DIR=q(E:\BackUp\BACKUP-FROM-W2K\ps\Digital-Multimedia\Images\2004\test);
my @array=split(/\n/,`dir $DIR /B`);
#
# Start printing the html page
#
print header;
print "<html><body><head><title>test</title></head>";
print "<table>";
for(my $i=0;$i<@array;$i++) {
print "<tr><td>$array[$i]</td></tr>";
}
print "</table></form></body></html>";

If I look at the source of the html page, I see everything else
printed, except

print "<tr><td>$array[$i]</td></tr>"; line

I have the script in the location http://127.0.0.1/cgi-bin/ and it has
required permission, I have other cgi-bin scripts in there which work
fine....

Vikram
 
S

Scott Bryce

Vikram said:
#!c:/Perl/bin/perl.exe
#
# Use the following modules
#
use CGI qw:)standard);
use strict;
use warnings;
#
# Define variables and create an array
#
my
$DIR=q(E:\BackUp\BACKUP-FROM-W2K\ps\Digital-Multimedia\Images\2004\test);

I assume this directory is on your hard drive.

my @array=split(/\n/,`dir $DIR /B`);
#
# Start printing the html page
#
print header;
print "<html><body><head><title>test</title></head>";
print "<table>";
for(my $i=0;$i<@array;$i++) {
print "<tr><td>$array[$i]</td></tr>";
}
print "</table></form></body></html>";

If I look at the source of the html page, I see everything else
printed, except

print "<tr><td>$array[$i]</td></tr>"; line

Could it be because it didn't find the directory on the server, so the
array was empty?
 
V

Vikram

The directiry is pretty much there, I am able to print the array on the
command line as I said before, it's only that this doesn't show up on
the web page. Infact, it even works fine on an unix machine ( FreeBSD
4.7, running apache2.0), but not in this scenario - as I already
mentioned I am using active perl on a windows xp.

Thanks
Vikram
 
A

A. Sinan Unur

my @array=split(/\n/,`dir $DIR /B`);

dir is an internal command. Try:

my @array=split(/\n/,`cmd /c dir $DIR /B`);

You should also check if your system command succeded:

C:\Home> set path=c:\perl\bin;

C:\Home> type mydir.pl
#! perl

use strict;
use warnings;

my @dir = `dir`;

print "$!\n";

C:\Home> mydir
Bad file descriptor

C:\Home> type mydir2.pl
#! perl

use strict;
use warnings;

my @dir = `c:\\windows\\system32\\cmd.exe /c dir`;

print @dir[0..10];

C:\Home> mydir2
Volume in drive C is HD
Volume Serial Number is F882-4E55

Directory of C:\Home\asu1

2005/01/11 10:18 PM <DIR> .
2005/01/11 10:18 PM <DIR> ..
2004/12/04 11:22 AM <DIR> .cpan
2005/01/11 05:56 PM <DIR> .gimp-2.0
2004/09/10 12:10 PM <DIR> .links
2003/01/30 01:10 PM <DIR> .ncftp
 
V

Vikram

I can run dir with out using cmd.exe as you suggested, the script
executes successfully when ran on the command line, it doesn't print
the array elements when invoked on the web page...
 
A

A. Sinan Unur

I can run dir with out using cmd.exe as you suggested, the script
executes successfully when ran on the command line, it doesn't print
the array elements when invoked on the web page...

Did you read my post?

Did you add the code to check if system succeeded? See

perldoc -f system

Did you print $! to see if there is some useful information there?

Do you have cygwin installed?

Sinan.
 
V

Vikram

It does succeed, that's what I have been saying right from the
beginning, if I invoke the script on the command prompt - it does print
everything perfectly, it takes the dir command, it prints all the
elements of the array,etc. But when it comes to invoking the same
script from the web, it doesn't print the array element portion of the
code, but it does print rest of the html body....

I did verify the code, it does work and I don't want to install cygwin,
that's the reason I am trying this out with dir command, I know I can
use unix commands if I install cygwin.
 
A

A. Sinan Unur

It does succeed, that's what I have been saying right from the

We have a communication problem here.

Clearly, something is failing when the script is run as CGI. Clearly, you
need to ask for as much information as possible in that case. Clearly, you
are refusing to do so. Obviously, you are not worth trying to help.
I did verify the code, it does work and I don't want to install cygwin,
that's the reason I am trying this out with dir command, I know I can
use unix commands if I install cygwin.

I am not suggesting that you should install cygwin. cygwin installs a
dir.exe which is an alias to ls.exe. If that dir.exe were in your path,
that might have explained why the script succeeds from the command line.

It is of course possible that I am missing something really obvious, but
there is no reason for

my @array = `dir`;

to succeed unless there is a dir.exe somewhere in your path.

Sinan
 
V

Vikram

Sinan,

I apologize if I have inconvinienced you, I understand you are trying
to help me here.

dir command on windows XP does work on the cgi scripts, I have noticed
that it doesn't work when it comes to windows 2000, I am not sure why
that is the case. I have tested your codes mydir.pl and mydir2.pl on
my machine and they do execute fine even if I don't use cmd.exe as you
have included in mydir2.pl.

If you are suggesting me that on the web it would be a different
scenario, then I might as well try to get dir.exe. But if it's
something that can be done without it, I would like to try it out...
 
A

A. Sinan Unur

If you are suggesting me that on the web it would be a different
scenario,

I am suggesting that it might be. I do not know which web server you are
using and what your configuration is.

And I would like you to check what if the system call succeeded when the
script is run as CGI and inspect and report $! if it did not. See

perldoc -f system
then I might as well try to get dir.exe.

I am not suggesting that.

Sinan.
 
V

Vikram

I did install mkstool kit and was able to us `ls -1` instead of dir
windows command, now it works, I presume your suggestion of using
cygwin might also work.

Thanks for all the help

Vikram
 
T

Tintin

Vikram said:
I am sorry not to have shown the entire script, however the script is
very simple, right now and is as follows:

#!c:/Perl/bin/perl.exe
#
# Use the following modules
#
use CGI qw:)standard);
use strict;
use warnings;
#
# Define variables and create an array
#
my
$DIR=q(E:\BackUp\BACKUP-FROM-W2K\ps\Digital-Multimedia\Images\2004\test);
my @array=split(/\n/,`dir $DIR /B`);
#
# Start printing the html page
#
print header;
print "<html><body><head><title>test</title></head>";
print "<table>";
for(my $i=0;$i<@array;$i++) {
print "<tr><td>$array[$i]</td></tr>";
}
print "</table></form></body></html>";

Why use an external command when you don't need to?

#!c:/Perl/bin/perl.exe
#
# Use the following modules
#
use CGI qw:)standard);
use strict;
use warnings;

print header;
print "<html><body><head><title>test</title></head>";
print "<table>";

foreach my $file
(<E:/BackUp/BACKUP-FROM-W2K/ps/Digital-Multimedia/Images/2004/test/*>) {
next unless -f $file;
print "<tr><td>$file</td></tr>\n":
}

print "</table></body></html>";


I haven't bothered fixing your HTML.
 
A

A. Sinan Unur

I did install mkstool kit and was able to us `ls -1` instead of dir
windows command, now it works, I presume your suggestion of using
cygwin might also work.

Provide some context. What are you replying to? I never suggested using
cygwin. I only asked if you had cygwin installed in trying to understand
the difference you were observing between command line operation versus CI
operation.

Sinan.
 
V

Vikram

Hi Tintin,

Thanks for your reply. Your solution works, indeed i don't need to use
the dos internal command here. This is what I was looking for and I
appreciate your help.

Vikram
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top