searc and replace

Discussion in 'Perl Misc' started by freedom, Apr 22, 2005.

  1. freedom

    freedom Guest

    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();
    }
     
    freedom, Apr 22, 2005
    #1
    1. Advertising

  2. freedom

    Joe Smith Guest

    freedom wrote:

    > 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
     
    Joe Smith, Apr 23, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais
    Replies:
    1
    Views:
    399
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    365
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,605
    Masudur
    Feb 18, 2008
  4. Prasad S
    Replies:
    2
    Views:
    250
    Dr John Stockton
    Aug 27, 2004
  5. Replies:
    3
    Views:
    185
    Brian McCauley
    Sep 12, 2005
Loading...

Share This Page