Well, hmmm... There are a couple levels of this problem to deal with. First, if you "use strict", a number of slip-ups appear in your code. I lost track of them, but here is a tidied-up version:
use strict; use CGI; use WWW::Search; my $q = new CGI; my $search = new WWW::Search ('AltaVista'); $search->maximum_to_retrieve(10); #my $word = $q->param('query'); my $word = 'penguin'; # for testing chomp($word); $search->native_query (WWW::Search::escape_query($word)); do_print($q, $search); sub do_print { my ($q, $search) = @_; print $q->header; print $q->start_html("Web Search"); print $q->h1({-align=>"center"},'Web Concordance Search Results'); print $q->h3({-align=>"center"},"for search term '$word'"); print $q->h4({-align=>"center"},"Producing output....\n"); print $q->hr; my $n = 0; while ( my $result = $search->next_result() ) { $n++; print $q->a({href=>$result->url}, $result->url); my $urlresult = $result->url; #my $result->description = @desc; my @desc = $result->description; my $desc = "@desc"; #To strip HTML tags crudely $desc = ~s(<[^>]*>)()g; my @splittext = split(/$word/,$desc); #To extract concordance lines from text for (my $i=1; $i < @splittext; $i++) { my $before = substr((' 'x10).$splittext[$i-1],-20,20); my $after = substr($splittext[$i].' 'x10,0,20); print p($before, strong($word), $after,"\n"), } print $q->br, $result->title, "\n"; print $q->br, $result->description,"\n"; print $result->change_date,"\n"; print $q->hr; } if ($n == 0) { print "<P>Results not found"; } print qq{<P><A HREF="http://mogana/index.htm">Search Again!</A>}; print $q->end_html; }
This runs (which is nice). Unfortunately it does not return any results for display. Since there were no results to process, I left your un-Perlish 'for' loop and some of the processing in that region of the code alone for now.

So we go through the docs looking for a simple example to try and end up with:

use strict; use WWW::Search; my $Search = new WWW::Search(); $Search->native_query(WWW::Search::escape_query('penguin')); while (my $Result = $Search->next_result()) { print $Result->url, "\n"; }
Which also runs and returns no results. Drat!

Curiously, in all the docs for this module, there seems to be no example I can find of what constitutes a proper argument for WWW::Search::escape_query( . . . ). I tried several things and nothing seemed to work. And I can't find anything here on PerlMonks or on the web.

So there we are... until someone else comes along who has some experience with this module, the script runs fine and appears quite harmless! ;-)


In reply to Re: Need help desperately on codes by dvergin
in thread Please review simple code to extract lines from a webpage description by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.