CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Obtaining information forms

 



alalleyn
Novice

Apr 18, 2002, 3:21 AM

Post #1 of 2 (401 views)
Obtaining information forms Can't Post

I have the following CGI script that should read an input file as obtained from a form and then append it to the bottom of xmlWeave.

use CGI;

$query = new CGI;

$file = $query -> param('file');

open (TXT, "<'$file'");
@text = <TXT>;
close (TXT);

open (DATA, ">>xmlWeave.xml");
foreach $line(@text) {
print DATA "$line";
}
close(DATA);

print "Content-type: text/html\n\n";
print "<html><h1>A Documentation file has been
created!\n</h1>";
print "Please open xmlWeave in a broswer to see complete documentation
file\n";
print "</html>\n";

However I am assumming that it is something I am doing wrong with the first few lines, as I have a script that works but this is when one has written in the name of the file to read into the script and so does not require any form input. The reason I am thinking that I am doing something wrong is because nothing appears to happen, in otherwords xmlWeave does not get appended to.

I was wondering is it because I am using POST to implement the form and then am not at any point reading STDIN. My understading was that the CGI module was able to deal with the post method for me and do all the spliting etc.

Hope someone can help me.


yapp
User

Apr 18, 2002, 9:57 AM

Post #2 of 2 (396 views)
Re: [alalleyn] Obtaining information forms [In reply to] Can't Post

- There are single quotes arround your filename!! open() fails! The script returns an error. The webserver doesn't send the message to the browser, since it's not a HTTP header. Bad luck i'd say. Unsure

use #!/usr/bin/perl -w
This is a great debugging tool!

If you have problems with understanding the error messages:
- use diagnostics;

It's recommended to declare all variables with my() first
use strict; forces you to do that!
my($x) = value;

use CGI::Carp qw(fatalsToBrowser)
- You'll see a lot of compiler messages (if you had any problems)

use: open(...) or die "can't open the file: $!";
- Not checking the result of any IO isn't good.



Don't forget the lock the file!
use Fcntl qw(:flock);
- before reading do:
flock(TXT, LOCK_SH);
- before writing do:
flock(TXT, LOCK_EX);
- before appending do:
flock(TXT, LOCK_EX);
seek(TXT, 0, 2) or die "Can't seek new EOF: $!"; # File size might changed after lock was released!


You're using the CGI module, why not using the header() method??? print $query->header('text/html');


So in other words:


Code
#!/usr/bin/perl -w 

use strict;
use diagnostics;
use CGI();
use CGI::Carp qw(fatalsToBrowser);
use Fcntl qw(:flock);

my $query = new CGI;

my $file = $query -> param('file');

open (TXT, $file) or die "Can't open $file: $!";
flock(TXT, LOCK_SH); # no one can edit the file now.
my @text = <TXT>;
close (TXT);

open (DATA, ">>xmlWeave.xml") or die "Can't open xml: $!";
lock(DATA, LOCK_EX); # waits until no-one uses the file, and blocks it then.
seek(DATA, 0,0) or die "Can't seek new EOF: $!";
print DATA @text; # all in once; print can handle array's
close(DATA);

print $query -> header('text/html');

print <<HTML_PAGE; # nice <<HERE; block
<html><h1>
A Documentation file has been created!</h1>
Please open xmlWeave in a broswer to see complete documentation file
</html>
HTML_PAGE


Voila! Wink

PS. what will happen if your users call your script like this:
script.cgi?file=/etc/passwd
This might be something you want to watch out for! Wink Good luck!

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

(This post was edited by yapp on Apr 18, 2002, 10:00 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives