Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > help! trouble reading / writing files in IIS

Reply
Thread Tools

help! trouble reading / writing files in IIS

 
 
Mad Scientist Jr
Guest
Posts: n/a
 
      04-01-2006
I am having trouble running some active perl in IIS5 under Win XP Pro.

I have activeperl installed, and perlscript is running - I got a
perlscript asp page to read a textbox from a form and do some simple
string comparisons, but am having trouble reading & writing files.

I have a file c:\inetpub\wwwroot\perl_read_file.txt that contains the
following text:

<h1>success</h1>
<p>read the file</p>

the script attempts to read.
The script also attemps to write to a file "perl_write_file.txt" in
wwwroot

The script is saved in c:\inetpub\wwwroot\perl_read_file.asp
and accessed at http://localhost/perl_read_file.asp

First I thought it might be permissions, but I have given the system
user ASP.NET full permissions to my c:\inetpub\wwwroot directory.
Unless there is some other user that the active perl stuff uses?

Here is the code (you will see many versions commented out, I have
tried different approaches)

Any help appreciated

<%@Language=PerlScript%>

<%

#
-----------------------------------------------------------------------------
# SET DEFAULTS
$sPage = "perl_read_file.asp";

#
-----------------------------------------------------------------------------
# GET VARIALBES FROM FORM
my($sFile) = $Request->Form('txtFile')->Item();

#
-----------------------------------------------------------------------------
# TEST FOR INPUT
if ($sFile eq '')
{
$sMessage = "Please type a file name and click [Submit].";
$sDefault = "perl_read_file.txt";
}
else
{
# $sMessage = "READ FILE: <br>";
#
# while ($myFile = <$sFile>)
# {
# open (FILE_IN, "$myFile") ;
# while ( $sNextLine=<FILE_IN> ) # read file one line at a time,
until end
# {
# $sMessage = join $sMessage, "\n", $sNextLine;
# }
# close (FILE_IN);
# }
#
# #my $filename = $Server->MapPath('/$sFile');
# $filename = $Server->MapPath('/$sFile');
#
# open (MYFILE, '$filename');
# while (<MYFILE>)
# {
# chomp;
# #print "$_\n";
# $sMessage = join $sMessage, $_, "\n";
# }
# close (MYFILE);
#
# #open( FILE, $Server->MapPath('/scripts/forum.asp') );
# #$sFile = join $Server->MapPath('/$sPage'), $sFile
# #$sFile = $Server->MapPath('$sPage')
#
# $sDefault = "$sFile";
# my $path = $Server->MapPath($Request->ServerVariables('PATH_INFO'));
# $sMessage = join $sMessage, "\n", $spath;
#my $path = $Server->MapPath($Request->ServerVariables('PATH_INFO'));
my $path = $Server->MapPath($sFile);
#$sMessage = $path;
open (MYFILE, '$path');
while (<MYFILE>)
{
chomp;
#$sMessage = join $sMessage, $_, "\n";
$sMessage = $sMessage . "x";
}
close (MYFILE);

} # else

#
-----------------------------------------------------------------------------
# WRITE A FILE
my $path = $Server->MapPath('perl_write_file.txt');
open (MYFILE, '>>$path');
print MYFILE "user requested $sFile\n";
close (MYFILE);

#
-----------------------------------------------------------------------------
# ENCODE OUTPUT
#$sMessage = $Server->HTMLEncode($sMessage);

%>

<HTML>
<TITLE>PerlScript Test Read File</TITLE>
<BODY>
<FORM ACTION="<% $Response->Write("$sPage"); %>" METHOD="POST">

<P>
<% $Response->Write("$sMessage"); %>
</P>
<TABLE>
<TR>
<TD align="right">File:</TD>
<TD align="left">
<INPUT NAME="txtFile" VALUE="<% $Response->Write("$sDefault");
%>">
<INPUT TYPE="SUBMIT" VALUE="Submit">
</TD>
</TR>
</TABLE>

</FORM>
</BODY>
</HTML>

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      04-01-2006

"Mad Scientist Jr" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>I am having trouble running some active perl in IIS5 under Win XP Pro.
>
> I have activeperl installed, and perlscript is running - I got a
> perlscript asp page to read a textbox from a form and do some simple
> string comparisons, but am having trouble reading & writing files.
>
> I have a file c:\inetpub\wwwroot\perl_read_file.txt that contains the
> following text:
>
> <h1>success</h1>
> <p>read the file</p>
>
> the script attempts to read.
> The script also attemps to write to a file "perl_write_file.txt" in
> wwwroot
>
> The script is saved in c:\inetpub\wwwroot\perl_read_file.asp
> and accessed at http://localhost/perl_read_file.asp
>
> First I thought it might be permissions, but I have given the system
> user ASP.NET full permissions to my c:\inetpub\wwwroot directory.
> Unless there is some other user that the active perl stuff uses?
>


PerlScript will run as the IUSR. Only aspx pages run as the ASP.NET user. I
don't think that's your problem, though.

> my $path = $Server->MapPath($sFile);


> open (MYFILE, '$path');


You have a really big problem right here. You're single-quoting your path,
so you're effectively telling Perl to try and open a file called $path, not
the actual path contained in the variable. This is one of the reasons you
should never needlessly quote variables in perl:

open(MYFILE, $path);

Since you're in an asp page, you might want to consider wrapping whatever
processing you do in an if conditional so you can do something else if the
open fails:

if (open(my $fh, '<', $path)) {
# do something with $fh
}

else {
$Response->write('oops. better do something else!');
}

Matt


 
Reply With Quote
 
 
 
 
Brad Murray
Guest
Posts: n/a
 
      04-01-2006
Mad Scientist Jr <(E-Mail Removed)> wrote:
MSJ> open (MYFILE, '$path');

Check the return from open -- I think you'll find it failed and why.

open (MYFILE, $path) or die "WTF?! $!";

--
Brad Murray * "When cryptography is outlawed, bayl bhgynjf jvyy
VSCA Founder * unir cevinpl." -- Kevin McCurley
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-01-2006
Mad Scientist Jr <(E-Mail Removed)> wrote:

> am having trouble reading & writing files.



> First I thought it might be permissions,



If you ask Perl to tell you why, then it will tell you why,
and you won't have to make random guesses like that.


> open (MYFILE, '$path');



You should always, yes *always*, check the return value from open():

open (MYFILE, $path) or die "could not open '$path' $!";


Have a look at the filename argument that you are passing to open

print('$path');

and I think you'll see another problem to fix.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      04-01-2006
Tad McClellan schreef:

> You should always, yes *always*, check the return value from open():


What would be a good way to automate that?

Maybe a variant on "use strict", like "use stricter" or "use akela".

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-01-2006
"Dr.Ruud" <(E-Mail Removed)> wrote in news:e0mcu2.q4.1
@news.isolution.nl:

> Tad McClellan schreef:
>
>> You should always, yes *always*, check the return value from open():

>
> What would be a good way to automate that?
>
> Maybe a variant on "use strict", like "use stricter" or "use akela".


http://search.cpan.org/~nwclark/perl-5.8.8/lib/Fatal.pm
--
--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc...uidelines.html

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      04-01-2006
A. Sinan Unur schreef:
> Dr.Ruud:
>> Tad McClellan:


>>> You should always, yes *always*, check the return value from open():

>>
>> What would be a good way to automate that?
>> Maybe a variant on "use strict", like "use stricter" or "use akela".

>
> http://search.cpan.org/~nwclark/perl-5.8.8/lib/Fatal.pm


Almost.

open/read/write/print/printf/close etc. would be shielded by default,
and on failures produce clear error messages, including the name of the
concerned file/pipe/etc. Fatal can almost do that, but rejects both
print and printf. Maybe these need to be overridden with a sub that uses
sprintf and write, to achieve this.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trouble writing results to files lisa.engblom@gmail.com Python 17 12-01-2006 10:15 AM
Reading and Writing files using Streamreader/StreamWriter Stephen ASP .Net 1 03-16-2006 11:57 AM
Reading/Writing pure binary files Daniel Gowans VHDL 2 06-12-2004 01:25 AM
Reading/writing data to/from files into 2D array Modukuri VHDL 0 05-19-2004 04:03 PM
Error: Could not find a part of the path / Reading and Writing to files in ASP.Net TC ASP .Net 3 05-19-2004 12:00 AM



Advertisments