search and replace help

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();
}
 
D

Dave Weaver

#!/usr/bin/perl

use strict;
use warnings;

A good start!
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
use LWP::UserAgent;
use HTML::TokeParser;

my $url = param( "url" );
my $option = param( "option");

Declare variables in the smallest possible scope, or as
late as possible. I would move the declaration of $option
lower down the code, or maybe remove it completely (since it's
only used once) and use:
if ( param( 'option' ) eq 'URL' ) {
later on in the code.
my $agent = new LWP::UserAgent();
my $request = new HTTP::Request( 'GET' => $url );

Similarly, if you are only using $url once, you could have written this as:
my $request = new HTTP::Request( 'GET' => param( 'url' ) );

my $response = $agent->request( $request );
my $document = $response->content();
my $page = new HTML::TokeParser( \$document );

This appears to be a CGI script. As such, its first output should be
the appropriate CGI headers. In your first script you remembered to write:
print( header() );

but you forgot to do so here.

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

I would write this as:
my ( $type, $url ) = @$token;
Functionally the same (in this case), but just my preference.
In fact you're not using this $url, so:
my ( $type ) = @$token;
would do just as well.

Also, you already have another variable called $url, which
can make your code confusing.
if ($type eq "T" ) {
print( "$url" );
}
}

$_ = param( "text" );

Better to use a named varaible here, rather than $_:
my $text = param( 'text');
$pattern = param( "pattern" );

if( $option == "URL" ) {

String comparisons require the string comparison operator, "eq".
my $string = get($url);

You are getting the same URL again?
Perhaps, with the multiple $url variables, you have a scoping
problem...
s/$_/$pattern/;

This substitution is operating on $_, i.e. it's the same as saying:
$_ =~ s/$_/$pattern/;
This is almost (but not quite) like saying: $_ = $pattern.
Perhaps you meant
$string =~ s/$_/$pattern/;
?
Note that, $pattern is ill-named - the right hand side of a s/// operator
is not a pattern (in the regex sense), just a string.

$pattern is never defined. You have "use strict" in your code which
means this code will never compile. Why have I just wasted my time
checking code that isn't your real code?

Please read the posting guidlines that are posted here regularly. They
will enable you to post your questions is such a way as to help you
get a good response from the knowledgeable regulars here. Failing to
comply with the posting guidlines for this group means your post will
probably be ignored by those most able to help you.
 
T

Tad McClellan

freedom said:
I m a newbie in perl


Have you seen the Posting Guidelines that are posted here frequently?

I generate an
html form with CGI.pm.


Not with the code that you've shown us you don't.

If you mislead us like that, we are likely to ignore your questions
or give an answer that you cannot use.

this is my generated form script


Errr, OK if you say so, but you also say that it runs when it won't run...

#!/usr/bin/perl
use strict;

<input name = "pattern" type = "text" value="$pattern" size = "50"></p>


You have a "use strict" violation there.

This isn't your real code (so what is the point of showing it to us?).

#!/usr/bin/perl
use strict;
use warnings;

if( $option == "URL" ) {


There is not much point in turning on warnings if you are just
going to ignore the messages that it produces...
 

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

Similar Threads

searc and replace 1
I dont get this. Please help me!! 2
Help with my responsive home page 2
search and replace in Perl 4
Help with code 0
Help please 8
Need help with this code 2
Search MySQL 2

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top