All pastes #911761 Raw Edit

utopia

public text v1 · immutable
·published 2008-02-20 22:19 UTC
rendered paste body
=for about
 	**************************************************
  	Author = Robert Gleeson 
        Title     = BSD::BSDoogle
 	E-mail = robert.gleeson3@gmail.com 
	WWW = http://www.denlab.cn
	**************************************************
 	Description
	 A Perl5 module that canused to interface with and search the Free,Net, and OpenBSD pkgsrc tree.  
=cut

	package BSD::BSDoogle;
	use strict;
	use warnings;

	use Exporter;
	our @ISA = ('Exporter');
	our @EXPORT = qw(&openindex &search);

	# variables global to the module.
	my @content;

	# This subroutine attempts to open the file(INDEX FILE);
	sub openindex
	{

	# Read input passed to openindex();
	my %input = @_;

	# Store passed input in $INDEX.
	my $INDEX = $input{'INDEX_FILE'};

	# Attempt to open the BSD INDEX file.
	open(IDX, '<', $INDEX) or return('0');
	@content = <IDX>;
	close(IDX);
	return ('1');
	} # END OF openindex();

	# This subroutine does the searching.
	sub search
	{

	# read input passed to search();
	my %input = @_;

	# Three arrays that hold the search results, the results are push()'ed into arrays later on.
	my (@titles,@locations,@descriptions);

	# Three scalar variables that hold the input passed to search();
	my ($title,$keyword,$case);

	# A variable that holds the regex to use for searching.
	my ($regex);

	# Check if the module was passed a title and/or a keyword to search for, if it has, add them to $title & $keyword 
	# accordingly..
	$title = $input{'TITLE'} if (exists($input{'TITLE'}) );
	$keyword = $input{'KEYWORD'} if(exists($input{'KEYWORD'}) );

	# Check if the search has been passed CASE_SENSITIVE='value', if it hasn't default to 'no' (case insensitive).
	if ( not exists($input{'CASE_SENSITIVE'} ) ) { $case = 'no' }
	else { $case = $input{'CASE_SENSITIVE'};  }

	# Loop through the content of INDEX file (@content).
        foreach my $entry (@content)
	{

	# split the INDEX file into $port,$location and $description
	my ($port,$location,undef,$description) = split(/\|/,$entry,4);
	
	
	# Make a case sensistive/insenstive search depending on the value of $case.
	# $case = 'no' means a case insensitive search, '1' means a case sensitive search.
	if($case eq 'no')
		{
	# TRUE if searching for just the title of a port.
	if(defined($title) && not defined($keyword) ) { $regex = $port =~ /$title/i;  }
	# TRUE if searching for  just the keyword/description of a port.
	elsif(defined($keyword) && not defined($title) ) { $regex = $description=~ /$keyword/i; }
	# TRUE if searching for both the keyword and description of a port.
	else { $regex = $port =~ /$title/i && $description =~ /$keyword/i;}
		}
	# do a case sensitive search.
	elsif($case eq 'yes')
		{
	#  TRUE if searching for just the title of a port.
	if(defined($title) && not defined($keyword) ) { $regex = $port =~ /$title/;  }
	# TRUE if searching for  just the keyword/description of a port.
	elsif(defined($keyword) && not defined($title) ) { $regex = $description=~ /$keyword/; }
	# TRUE if searching for both the keyword and description of a port.
	else { $regex = $port =~ /$title/i && $description =~ /$keyword/;} 
		}
	
	#true if there was a match in the search.
	# Time to add the results into an array of found ports.
	if($regex) { push (@titles,$port);
			push (@descriptions,$description);
			push(@locations,$location); 
	}
	
	} # END OF foreach();

	
	return(\@titles,\@descriptions,\@locations);

	} # END OF search();

	

1;