searc and replace

F

freedom

I m a newbie in perl and just try to finish an assignment. I generate an
html form with CGI.pm. If the user enter a url in the first textfield, that
url supposed to be loaded wit LWP. In the second text field if the user
enter a tag or a filename it is supposed to search and it should be replaced
whatever user entered in the third field.
The problems:
1.I m getting text document instead of html source code when i enter a url
in the first textfield..
2. I can not do the search and replace in the second and third field

any help please. I will be so thankful

this is my generated form script
#!/usr/bin/perl
use strict;
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);

print( header() );
print( start_html(-title => 'Online XHTML Change Form' ) );

print <<FORM;
<form action = "start.cgi" method = "post">
<p>Option:
<select name = "option">
<option selected>URL</option>
<option>File</option>
<option>Tag</option>
</select>
<p>URL of page to be changed:
<input name = "url" type = "text" size = "50"></p>
<p>Name of file, tag or url to be changed:
<input name = "text" type = "text" value="$_" size = "50"></p>
<p>Name to be changed:
<input name = "pattern" type = "text" value="$pattern" size = "50"></p>
<input type = "submit" value = "submit">
</form>
FORM

print( end_html() );


this is the html parsing , search and replace. I called it start.cgi

#!/usr/bin/perl

use strict;
use warnings;
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
use LWP::UserAgent;
use HTML::TokeParser;

my $url = param( "url" );
my $option = param( "option");
my $agent = new LWP::UserAgent();
my $request = new HTTP::Request( 'GET' => $url );
my $response = $agent->request( $request );
my $document = $response->content();
my $page = new HTML::TokeParser( \$document );

while ( my $token = $page->get_token() ) {
my $type = shift( @{ $token } );
my $url = shift( @{ $token } );

if ($type eq "T" ) {
print( "$url" );
}
}

$_ = param( "text" );
$pattern = param( "pattern" );

if( $option == "URL" ) {
my $string = get($url);
s/$_/$pattern/;
}

if (!$url ) {
print( start_html() );
print( h4( "Url field should not be blank." ) );
print( h4("Please try again.") );
print( "<a href = \"/assign2.cgi\">Go back</a>" );
print( end_html() );
exit();
}
 
J

Joe Smith

freedom said:
1.I m getting text document instead of html source code when i enter a url
in the first textfield..

What do you mean by that? Are you saying that
$document = $response->content();
is not returning a string of text?
2. I can not do the search and replace in the second and third field
s/$_/$pattern/;

There's a big difference between
$_ =~ s/$_/$pattern/;
and
$string =~ s/$_/$pattern/;
or even
$string =~ s/$search_for/$replace_with/g;

Which one are you trying to accomplish?

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top