Mattk470 has asked for the wisdom of the Perl Monks concerning the following question:

I have this code that works when I run it locally but not when I run it as a CGI program. I was going to use it to read from a file then overwrite it's contents with something else. have any suggestions why it works on one and not the other or a better way of doing it? P.S. I can however, write to the file if I open it for writing before I open it for reading so it doesn't seem to be file permissions.
my $config = 'e:\Inetpub\wwwroot\msdata\config.txt'; open (CON, $config); my @list = <CON>; close CON; my $list = join( '', @list ); @list = split(/=/, $list); my $testnum = $list[1]; open (CONOUT, ">".$config); print CONOUT '[filenumber]=1'; close CONOUT;

Replies are listed 'Best First'.
Re: my program will run on my computer but not in CGI
by greenFox (Vicar) on Feb 01, 2005 at 02:41 UTC
Re: my program will run on my computer but not in CGI
by legato (Monk) on Feb 01, 2005 at 02:37 UTC

    Most web servers run as their own special user, some don't even respect setuid bits on CGI applications. Most likely, the user your web server is running as (and therefore, the user your script is running as when it is CGI) doesn't have the rights to modify/replace the file. Check the file permissions, and those on the directories containing it.

    Also, for future reference, a better description of what is not working for you (i.e. sample output, description of when/how the error occurs, etc.) will be more helpful than "it doesn't work". If we all know how it doesn't work, we'll be much better equipped to tell you why.

    Anima Legato
    .oO all things connect through the motion of the mind

Re: my program will run on my computer but not in CGI
by Popcorn Dave (Abbot) on Feb 01, 2005 at 04:29 UTC
    Are you sure that your permissions are set correctly? Since I primarily work with windows, that's the one thing that I get stung on when doing CGI. I believe that you want to set them to 755. Your ftp client should be able to do that for you.

    Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.
Re: my program will run on my computer but not in CGI
by fauria (Deacon) on Feb 01, 2005 at 02:44 UTC
    To ensure it is not a wrong data handling trouble (its likely to be a write privileges problem), what about using XML for config files? For the config file, you can try something like:

    <opt>
      <section1>
       <param1>foo</param1>
       <param2>bar</param2>
      </section1>
      <section2>
       <other>foobar</other>
      </section2>
    </opt>

    And then, use something like this to read, modify and update your config file:

    use strict; use XML::Simple; my $config = "fooconf.xml"; my $xml = XMLin($config) or die "Error reading config: $!"; $xml->{section1}{param1} = "replacement1"; $xml->{section2}{other} = "replacement2"; open CONF, ">$config" or die "Error writing config: $!"; print CONF XMLout($xml, NoAttr => 1); close CONF;
Re: my program will run on my computer but not in CGI
by cog (Parson) on Feb 01, 2005 at 08:34 UTC
    Make sure you use warnings and use strict, and than, when you try to run it as a CGI program, look at the server's error log (there is one, right?)
Re: my program will run on my computer but not in CGI
by Anonymous Monk on Feb 02, 2005 at 00:25 UTC
    Some possible debugging advice: are you printing the content header? print "Content-type: text/html\n\n"; turn on warnings: (use -w) use strict; die "File not found unless" -f $config; open(CON,$config) || die $!; my $list = join '', <CON>; ... open(CONOUT, ">$config") || die $!; Also consider using flock, or using some other configuration method (berkely DB [DB_File] or XML [xml::Simple] )