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

Hy! I have a problem.
Why the botton program can't write a database on Windows Me, Xp?
At my computer none program writted in Perl can't read or write a database (.txt or .db).
Why? What is happening? Please help me. I use Sambar Server or Abyss Web Server for view.
#!/usr/bin/perl if ($ENV{'REQUEST_METHOD'} eq 'GET') { $buffer = $ENV{'QUERY_STRING'}; } else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } $path = "C:\Program Files\sambar51\cgi-bin"; open(TEST, ">>$path/text.txt") || die ("I can't write in database!"); print TEST "$FORM{'text'}\n"; close(TEST); print "Content-type: text/html\n\n"; print "OK. The text is in text.txt"; exit;
Thank you!

edited: Sun Feb 9 14:24:38 2003 by jeffa - code tags, title change (was: Whay?)

Replies are listed 'Best First'.
Re: Need help with writing to db file
by tachyon (Chancellor) on Feb 09, 2003 at 14:34 UTC

    As an aside you should ditch the hand rolled param parser, see use CGI or die; for more details. You can replace all this:

    if ($ENV{'REQUEST_METHOD'} eq 'GET') { $buffer = $ENV{'QUERY_STRING'}; } else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; }

    With this which is much more reliable as well as shorter...

    use CGI 'Vars'; my %FORM = Vars();

    I would also recomend reading Use strict, warnings and diagnostics or die as well as New Monks

    Update

    Imported Vars() method specifically as it is not part of the ':standard' set as pointed out by Coruscate

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Perhaps this depends on the version of CGI, but 2.81 nor 2.89 at least do not export Vars() by default. You have to implicitly import ':cgi-lib' to call Vars() without using an object. That leaves us with either one of the following:

      use CGI qw/:standard :cgi-lib/; my %FORM = Vars(); # OR use CGI ':standard'; my $q = new CGI; my %FORM = $q->Vars(); # Oddly enough, the following do NOT work: use CGI ':standard'; my %FORM = CGI->Vars(); # or my %FORM = CGI::Vars();


      If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, reply to this node or /msg me to tell me what is wrong with the post, so that I may update the node to the best of my ability. If you do not inform me as to why the post deserved a downvote, your vote does not have any significance and will be disregarded.

        You are correct that you need to specifically import the Vars() method as it is not part of the ':standard' set. A simple use CGI 'Vars'; will do. My mistake as I always use the OO interface.

        Without wanting it to look like sour grapes for you excellent correction of my incorrect code.....a pet peve of mine is people who do this:

        use CGI ':standard'; my $q = new CGI; $q->method();

        When you use the OO interface of CGI or CGI::Simple you don't need to import anything so just use CGI; is fine.

        BTW - The reason that the CGI->Vars() code does not work relates to the convoluted way in which CGI.pm works and specifically to how it instantiates the internal object.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Need help with writing to db file
by Cabrion (Friar) on Feb 09, 2003 at 14:25 UTC
    $path = "C:\Program Files\sambar51\cgi-bin";
    should read
    $path = "C:\\Program Files\\sambar51\\cgi-bin";
    or
    $path = "C:/Program Files/sambar51/cgi-bin";
      Thank you. Thank you. Thank you. Thank you.
      Its work with $path = "C:/Program Files/sambar51/cgi-bin";
      Thank you.Thank you.Thank you.Thank you.
Re: Need help with writing to db file
by tachyon (Chancellor) on Feb 09, 2003 at 14:27 UTC

    You need to change this:

    $path = "C:\Program Files\sambar51\cgi-bin"; # to either $path = "C:\\Program Files\\sambar51\\cgi-bin"; # or $path = "C:/Program Files/sambar51/cgi-bin";

    See Paths in Perl for more details. Also when you open a file the error (if any) is stored in $! so you get more info if you do:

    open FILE, $file or die "Can't open $file, perl says error is $!";

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print