Operating System - HP-UX
1753835 Members
8552 Online
108806 Solutions
New Discussion юеВ

Another Perl CGI Question

 
SOLVED
Go to solution
Jason Berendsen
Regular Advisor

Another Perl CGI Question

Why is it that if I run the following code from the command prompt it will read the input file and output the array value at 1, but when I try to access it from my browser it won't show any value?

#!/opt/perl/bin/perl
$inx=0;

open ONCALL, ("/usr/local/apache/cgi-bin/unixoncall.txt" or die "Counld not open file!\n");
while () {
chomp;
@oncall[$inx]="$_";
$inx++;
}

print "Content-type:text/html\n\n";
print "ONCALL";

print "

$oncall[1]



";

print "";
4 REPLIES 4
Bill McNAMARA_1
Honored Contributor
Solution

Re: Another Perl CGI Question

try it as user www

Later,
Bill
It works for me (tm)
Jason Berendsen
Regular Advisor

Re: Another Perl CGI Question

I suppose it would help to look at what permissions the file you are reading in has!

Thanks,

Jason
Gregory Fruth
Esteemed Contributor

Re: Another Perl CGI Question

Your call to open() is very strange; perhaps you meant:

open (ONCALL, "/usr/local/apache/cgi-bin/unixoncall.txt") or
die "Counld not open file!\n";

As you've written it, if open() fails the script continues
to execute. The die() only gets called if the filename is
not true.

Also, your use of:

@oncall[$inx]="$_";

should probably be:

$oncall[$inx]="$_";

Also note that Perl array indices (normally) start at zero
and not one.
Ralph Grothe
Honored Contributor

Re: Another Perl CGI Question

Hi Jason,

Gregory is absolutely right.
Your code exhibits some obscurities, which I think are only due to some unfamiliarity with Perl.
First your open() call is strange indeed, but works because open() also accepts a list after its first argument which is a filehandle. (please read the open() POD, viz. "perldoc -f open").
The round parentheses depict a list, which in your case has as its only element the path to unixconcall.txt.
Since your or'ed the path string (which is always defined) to a die() call that never will be executed, your list has only this one element which is a valid filename.
Thus the Perl compiler won't complain even if you ran it with warnings enabled.
You have to choices.
Either you omit the parentheses altogether (which is a bit more like the sloppy Perlish style)
e.g.

open ONCALL, '/usr/local/apache/cgi-bin/unixoncall.txt'
or die "cannot open: $!\n";

or (as preferred by people coming from more strict languages), you embrace the two arguments with a pair of parentheses

e.g.

open(ONCALL, '/usr/local/apache/cgi-bin/unixoncall.txt')

Then the assignment

@oncall[$inx]="$_";

actually is an assignment to a slice.
I really don't believe that you indended this, because it doesn't make much sense in this context.
Remember, Perl basically distinguishes between two "types", either list or scalar context.
Whenever you reference a single element of an array you address it in scalar context, thus requiring the '$'

$oncall[$inx] = "$_";

Apart from this, if you really afford to squeeze the whole file in an array you can have this much easier by enforcing list context with the diamond operator

e.g.

@oncall = ;

However, this is not very resource efficient, and you should rather loop through the lines of a file.

If you mean to address the first element of a list, you will have to use the index 0 unless you redefined the special Perl variable '$[' (which is really deprecated)
Read the POD "perldoc perlvar".

As far as CGI is concerned you'd better use the CGI.pm module (which is easier and *safer*)
Read "perldoc CGI".

For tampering with files also read
"perldoc perlopentut"

And finally, I'd strongly recommend to read the following:

"perldoc perldata"
"perldoc perlsyn"
Madness, thy name is system administration