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

Hi,
I am writing a perl script to work on web. I dont have any problem while displaying static data. I modufied my script to acces a file and display the contents. On command line the script works fine. But when I open the script using web page on Apache server I am unable to display the contents of the target file. Funny part is, except the data from file I am getting entire static data on the web page.


Please help me to solve this problem.


#!/usr/bin/perl my $cfgfile="./dbconnect"; open (CONFIG,$cfgfile); while (<CONFIG>) { chomp; # no newline s/#.*//; # no comments s/^\s+//; # no leading white s/\s+$//; # no trailing white next unless length; # anything left? my ($var, $value) = split(/\s*=\s*/, $_, 2); $value=~ s/\r//g; $ConfigValue{$var} = $value; } print "Name--$ConfigValue{'NAME'}";

--------------------
The above script works fine on CLI but on web I get
only "Name--" displayed.

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: Problem while displaying dynamic data on perl CGI pages
by MonkE (Hermit) on Jun 01, 2007 at 11:48 UTC
    When you run the script through Apache, it will run as a different user ID than you are (presumably) using on the command line. Are you sure that the user ID that Apache uses has the proper access to that file. For instance on Ubuntu, Apache runs as user www-data: does user www-data have access to the file?
      Files are created under roor:root but the file permissions ar set to 755.
      Do I need to change the owner:group of the files?
        Hi,
        Changed the file owner and it started working.
        Thanks for the tip.
Re: Problem while displaying dynamic data on perl CGI pages
by holli (Abbot) on Jun 01, 2007 at 11:48 UTC
    The current working directory (cwd) of a script invoked by the webserver can differ from the dir your script resides. So when you use relative paths in your script (here ".") and you're not taking the cwd into account you're prone to errors. To make that clearer, open a shell to your script name, cd up (cd ..), then type "directory/scriptname" and see that is doesn't work.

    To see what your cwd in the web environment is use the Cwd-Module:
    use Cwd; print "currently working from: ", getcwd(), "\n";
    Then you can change the path in your script or move the datafile accordingly.


    holli, /regexed monk/
Re: Problem while displaying dynamic data on perl CGI pages
by friedo (Prior) on Jun 01, 2007 at 11:48 UTC
    Check the return value from your open statement. Your webserver probably doesn't have adequate permissions for the file. You should also run with strict and warnings enabled.
    open (CONFIG,$cfgfile) or die "Unable to open $cfgfile: $!";
      File is opening, No error message displayed. I tried once by giving wrong file name which threw an error.
Re: Problem while displaying dynamic data on perl CGI pages
by moritz (Cardinal) on Jun 01, 2007 at 15:18 UTC
    If you do an open, always check if it worked.

    That catches many "mysterious" errors.