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: Beginner:
Beginner needs help writing to file using Perl...

 



Angi_28_CA
Deleted

Apr 8, 2000, 10:51 PM

Post #1 of 2 (1147 views)
Beginner needs help writing to file using Perl... Can't Post

Hi,

I should mention that I am a beginner. I would also like
to apologize if anyone has seen this message already, as
I posted on a few other boards as well. I am on an NT server,
running Perl 5.

Although it appears that the script works, there are no
errors, and the redirection works as planned. LWP::Simple;
does work. I was able to print the page to the screen. I
want to print it to a file instead of the page. The file
it should print to (and the url to grab) is being sent
via a form. The file does exist. I hope that someone can
offer some suggestions. BTW, I also tried entering the actual
filename and a line of text to write to the file, just to
test it, and it did not work either. My file remains blank!

Here is the code I am using:

#!/perl/bin/perl

use LWP::Simple;

&get_form;

$URL = $INPUT{'url'};

$TEMPFILE = $INPUT{'tempfile'};

$KEYW1 = $INPUT{'keyw1'};

$KEYW2 = $INPUT{'keyw2'};

$KEYW3 = $INPUT{'keyw3'};

$REDIRECTURL = 'testred.asp';

$ERRREDIRECTURL = 'metalyzerr.asp';

$CONTENT = get($URL);

if (head($URL)) {

print "Location: $ERRREDIRECTURL\n\n";

} else {

open (HANDLE, ">>$TEMPFILE");

print HANDLE $CONTENT;

close (HANDLE);

print "Location:

$REDIRECTURL?url=$URL&filename=$TEMPFILE&keyw1=$KEYW1&keyw2=$KEYW2&keyw3=$KEYW3\n\n";

}

exit;

sub get_form {

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;

if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }

else { $INPUT{$name} = $value; }

}

}

Thank you in advance,

Angi


Jasmine
Administrator / Moderator

Apr 10, 2000, 2:47 PM

Post #2 of 2 (1147 views)
Re: Beginner needs help writing to file using Perl... [In reply to] Can't Post

It's a simple fix...

change

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if(head($URL)) {
</pre><HR></BLOCKQUOTE>

to

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


unless (head($URL)) {
</pre><HR></BLOCKQUOTE>

The problem was that you were redirecting to the "error" page if the head function returned a value (which means that LWP snatched the page).

The way you had (with if instead of unless) would print only if LWP was unsuccessful.

I've tried it with unless and it worked like a charm.

Good luck!

 
 


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

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