search and replace help

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

    Dave Weaver Guest

    On Fri, 22 Apr 2005 01:11:19 -0400, freedom <> wrote:

    > #!/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.
    Dave Weaver, Apr 22, 2005
    #2
    1. Advertising

  3. freedom <> wrote:

    > 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...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 22, 2005
    #3
    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. techie2go
    Replies:
    3
    Views:
    258
    techie2go
    Sep 10, 2006
  2. Schif Schaf
    Replies:
    12
    Views:
    1,247
    Anthra Norell
    Feb 8, 2010
  3. Abby Lee
    Replies:
    5
    Views:
    375
    Abby Lee
    Aug 2, 2004
  4. Prasad S
    Replies:
    2
    Views:
    214
    Dr John Stockton
    Aug 27, 2004
  5. Ray Muforosky

    string substitution help (search and replace)

    Ray Muforosky, Mar 9, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    118
Loading...

Share This Page