WWW::SearchResult - class for results returned from WWW::Search


WWW-Search documentation Contained in the WWW-Search distribution.

Index


Code Index:

NAME

Top

WWW::SearchResult - class for results returned from WWW::Search

SYNOPSIS

Top

    require WWW::Search;
    require WWW::SearchResult;
    $search = new WWW::Search;
    $search->native_query(WWW::Search::escape_query($query));
    # Get first result:
    $result = $search->next_result();

DESCRIPTION

Top

A framework for returning the results of WWW::Search.

SEE ALSO

Top

WWW::Search

REQUIRED RESULTS

Top

The particular fields returned in a result are backend- (search engine-) dependent. However, all search engines are required to return a url and title. (This list may grow in the future.)

METHODS AND FUNCTIONS

Top

new

To create a new WWW::SearchResult, call

    $result = new WWW::SearchResult();

url

Returns the primary URL. Note that there may be a list of urls, see also methods urls and add_url. Nothing special is guaranteed about the primary URL other than that it is the first one returned by the back end.

Every result is required to have at least one URL.

add_url

Add a URL to the list.

urls

Return a reference to the list of urls. There is also a primary URL (url).

title, description, score, change_date, index_date, size, raw

Set or get attributes of the result.

None of these attributes is guaranteed to be provided by a given backend. If an attribute is not provided its method will return undef.

Typical contents of these attributes:

title

The title of the hit result (typically that provided by the 'TITLE' HTML tag).

description

A brief description of the result, as provided (or not) by the search engine. Often the first few sentences of the document.

source

Source is either the base url for this result (as listed on the search engine's results page) or another copy of the full url path of the result. It might also indicate the source site name or address whence the result came, for example, 'CNN' or 'http://www.cnn.com' if the search result page said "found at CNN.com".

This value is backend-specific; in fact very few backends set this value.

add_sources

Same meaning as source above, for adding sources in case there are potentially multiple sources.

sources

Returns a reference to the list of sources.

score

A backend specific, numeric score of the search result. The exact range of scores is search-engine specific. Usually larger scores are better, but this is no longer required. See normalized_score for a backend independent score.

normalized_score

This is intended to be a backend-independent score of the search result. The range of this score is between 0 and 1000. Higher values indicate better quality results.

This is not really implemented since no one has created an backend-independent ranking algorithm.

change_date

When the result was last changed. Typically this is the modification time of the destination web page.

index_date

When the search engine indexed the result.

size

The approximate size of the result, in bytes. This is only an approximation because search backends often report the size as "18.4K"; the best we can do with that number is return it as the value of 18.4 * 1024.

raw

The raw HTML for the entire result. Raw should be exactly the raw HTML for one entry. It should not include list or table setup commands (like ul or table tags), but it may include list item or table data commands (like li, tr, or td). Whether raw contains a list entry, table row, br-separated lines, or plain text is search-engine dependent. In fact, many backends do not even return it at all.

as_HTML

Convert the search result to a human-readable form, decorated with HTML for pretty-printing.

Others

More attributes of the result. Backend-specific. Refer to the documentation of each backend for details.

bid_amount
bid_count
bidder
category
company
end_date
image_url
item_number
location
question_count
seller
shipping
sold
start_date
thumb_url
watcher_count

AUTHOR

Top

WWW::SearchResult was written by John Heidemann. WWW::SearchResult is maintained by Martin Thurn.


WWW-Search documentation Contained in the WWW-Search distribution.
# SearchResult.pm
# by John Heidemann
# Copyright (C) 1996 by USC/ISI
# $Id: SearchResult.pm,v 2.78 2008/07/21 01:20:30 Martin Exp $
#
# Copyright (c) 1996 University of Southern California.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that the above copyright notice and this paragraph are
# duplicated in all such forms and that any documentation, advertising
# materials, and other materials related to such distribution and use
# acknowledge that the software was developed by the University of
# Southern California, Information Sciences Institute.  The name of the
# University may not be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.


#####################################################################

package WWW::SearchResult;

use strict;
use warnings;

use CGI;
use base 'LWP::MemberMixin';
our
$VERSION = do{ my @r = (q$Revision: 2.78 $ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r};

sub new
  {
  my $class = shift;
  my $self = bless { }, $class;
  $self->{urls} = ();
  return $self;
  } # new

sub url
  {
  my $self = shift;
  if (@_ < 1)
    {
    # No arguments, just return the current value:
    return ${$self->{urls}}[0];
    } # if no args
  unshift @{$self->{urls}}, $_[0];
  return $self->{urls}->[0];
  } # url

sub _elem_array
  {
  my $self = shift;
  my $elem = shift;
  if (@_ < 1)
    {
    # No arguments
    return wantarray ? @{$self->{$elem}} : $self->{$elem};
    } # if
  if (ref($_[0]))
    {
    # Trying to assign an arrayref:
    $self->{$elem} = $_[0];
    }
  else
    {
    # Trying to set to a scalar (or list of scalars); make sure it's
    # an array even if they give one element:
    $self->{$elem} = undef;
    push @{$self->{$elem}}, @_;
    }
  # Always return array refrence
  return $self->{$elem};
  } # _elem_array

sub _add_elem_array
  {
  my $self = shift;
  my $elem = shift;
  # No matter how many they're adding:
  push(@{$self->{$elem}}, @_);
  } # _add_elem_array


sub add_url { return shift->_add_elem_array('urls', @_); }

sub urls { return shift->_elem_array('urls', @_); }

sub add_related_url { return shift->_add_elem_array('related_urls', @_); }

sub related_urls { return shift->_elem_array('related_urls', @_); }

sub add_related_title { return shift->_add_elem_array('related_titles', @_); }

sub related_titles { return shift->_elem_array('related_titles', @_); }


sub title { return shift->_elem('title', @_); }

sub description { return shift->_elem('description', @_); }

sub source { return shift->_elem('source', @_); }

sub add_sources { return shift->_add_elem_array('sources', @_); }

sub sources { return shift->_elem_array('sources', @_); }

sub score { return shift->_elem('score', @_); }

sub normalized_score { return shift->_elem('normalized_score', @_); }

sub change_date { return shift->_elem('change_date', @_); }

sub index_date { return shift->_elem('index_date', @_); }

sub size { return shift->_elem('size', @_); }

sub raw { return shift->_elem('raw', @_); }

sub as_HTML
  {
  my $self = shift;
  my $cgi = new CGI;
  my $s = $cgi->a({
                   href => $self->url,
                  },
                  $self->title || 'title unknown',
                 );
  $s .= $cgi->br;
  $s .= $self->description || 'no description available';
  return $s;
  } # as_HTML

sub bid_amount { return shift->_elem('bid', @_); }

sub bid_count { return shift->_elem('bids', @_); }

sub bidder  { return shift->_elem('bidder', @_); }

sub category { return shift->_elem('category', @_); }

sub company { return shift->_elem('company', @_); }

sub end_date { return shift->_elem('end_date', @_); }

sub image_url { return shift->_elem('image_url', @_); }

sub item_number { return shift->_elem('item_number', @_); }

sub location { return shift->_elem('location', @_); }

sub question_count { return shift->_elem('question_count', @_); }

sub seller { return shift->_elem('seller', @_); }

sub shipping { return shift->_elem('shipping', @_); }

sub sold { return shift->_elem('sold', @_); }

sub start_date { return shift->_elem('start_date', @_); }

sub thumb_url { return shift->_elem('thumb_url', @_); }

sub watcher_count { return shift->_elem('seller', @_); }

1;

__END__