help! trouble reading / writing files in IIS

Discussion in 'Perl Misc' started by Mad Scientist Jr, Apr 1, 2006.

  1. 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>
    Mad Scientist Jr, Apr 1, 2006
    #1
    1. Advertising

  2. Mad Scientist Jr

    Matt Garrish Guest

    "Mad Scientist Jr" <> wrote in message
    news:...
    >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
    Matt Garrish, Apr 1, 2006
    #2
    1. Advertising

  3. Mad Scientist Jr

    Brad Murray Guest

    Mad Scientist Jr <> 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
    Brad Murray, Apr 1, 2006
    #3
  4. Mad Scientist Jr <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 1, 2006
    #4
  5. Mad Scientist Jr

    Dr.Ruud Guest

    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."
    Dr.Ruud, Apr 1, 2006
    #5
  6. "Dr.Ruud" <> 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 <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Apr 1, 2006
    #6
  7. Mad Scientist Jr

    Dr.Ruud Guest

    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."
    Dr.Ruud, Apr 1, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Jeurissen
    Replies:
    5
    Views:
    428
  2. Replies:
    17
    Views:
    531
    Peter Otten
    Dec 1, 2006
  3. Ben
    Replies:
    3
    Views:
    93
  4. Rod Richenberg

    help reading/writing from/to excel files

    Rod Richenberg, Jan 23, 2007, in forum: Ruby
    Replies:
    1
    Views:
    109
    Glen Pfeiffer
    Jan 23, 2007
  5. Replies:
    2
    Views:
    80
    Robert Klemme
    Apr 20, 2009
Loading...

Share This Page