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

Hello monks just another question for you about Tie::File.
I'm writing a program for a flat database and i made a little module.
It includes Tie::File module.
The program works well if it is a command line executable but it fails if it is a CGI because it can't tie my file and the $Card reference doesn't contain anything...
Any ideas?
the code in CGI
use CGI; use Gnome::Card; my $q = new CGI; print $q -> header("text/html"), $q -> start_html(-'title'=>"Example",), $q -> h1("Programma di gestione remota GnomeCard"); my $filecard="../GnomeCard.gcrd"; my $Card=Leggi($filecard); print "$Card->[2] is good.. \n"; print $q -> end_html();
the code in module Gnome::Card
sub Leggi{ my @Card; my $filecard=shift; tie @Card, 'Tie::File', $filecard; ... return \@Card; }
PS: GnomeCard is the module that i wrote
PS: The module is propely installed
PS: i've the privileges in reading file and if i put this code in CGI it prints all rows:
open FILE, "<$filecard"; while(<FILE>) {print $_,"<br>";}

-­--
os: Linux Mandrake 9.0

Replies are listed 'Best First'.
Re: Tie::File and CGI
by shemp (Deacon) on Aug 04, 2003 at 17:36 UTC
    It looks like a path issue to me. When you run the script from the command line, your cwd for the program is different from when you run from the CGI. The line:
    my $filecard="../GnomeCard.gcrd";
    is very suspect because it includes a relative path. If you add to your script a die statement if the tie fails, that may help you catch the error, such as:
    tie @Card, 'Tie::File', $filecard or die "couldnt tie $filecard as a Tie::File $!\n";
    This way, the reason the tie didnt work will be sitting in your error log.

    Its generally important to keep in mind that cgi scripts will run under a different environment when run as a cgi, as opposed to running from the command line.

    Unless you have a very specific reason, commands like tie() or open() should always have failure tests - it will save you many headaches in the future.

    Not directly related to the question, but you open statement should also have error checking:
    open FILE, "<$filecard" or die "couildnt open $filecard for reading $!\n";
Re: Tie::File and CGI
by RMGir (Prior) on Aug 04, 2003 at 17:36 UTC
    This is just a guess, but you're specifying "$filecard" to be a relative path, "../GnomeCard.gcrd".

    Is it possible the current working directory isn't what you expect when you're running as a CGI?

    Try putting an absolute path in for $filecard, or doing an explicit "chdir" early in the script to where you expect to be.
    --
    Mike