#!/home/rebelsky/perl/bin/perl

### Jared Seaman     6/29/99
### A package module that contains Sam's slashify,
### my searchsee, and the 3 testing programs applicable thereto

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

package Searchslash;
use Exporter ();
@ISA = (Exporter);
@EXPORT = qw(slashify backslash);

# Recommendation: Look at Sam's Perl code for some coding
# techniques.  In particular, always define a main() routine, and
# call that.
# Test of slashify.
sub testslash {
  while (<STDIN>) {
    chop($_);
    $slashified = slashify($_);
    print "Converted '$_' to '$slashified'\n";
  } # while
  exit 0;
} # testslash


# Test of searchsee, mainly copied from Sam's testslash
sub testsearchsee {
  print "What would you like to look at through Search's eyes?  ";
  while (<STDIN>) {
    chop($_);
    my ($fixed) = searchsee($_);
    print "\nConverted '$_' to \n'$fixed'\n";
  } # while
  exit 0;
} # testsearchsee


#textsearchtest
#this is a Text-to-Search tester:
#It slashifies something, then makes sure this looks right to a searcher.
sub textsearchtest {
  print "What do you want to look at?  ";
  while (<STDIN>) {
    chop($_);
    print "A search will see that & say:\n\n".&searchsee(&slashify($_))."\n\n";
  }
  exit 0;
} # textsearchtest



#Slashify by Sam Rebelsky, 6/13/99
# Given a text string that will be used as a pattern, 
# put a backslash before any pattern symbols.
sub slashify {
  # Get the parameters
  my $str = $_[0];
  # Backslash the slashes first, so that if we insert any others,
  # they don't get slashified.
  $str =~ s/\\/\\\\/g;
  # The important symbols are listed on page 59 of the book.
  # For each such symbol
  foreach $sym (split(//,"|()[{^$+?.")) {
    $sym = "\\" . $sym;
    # Replace all copies of the particular symbol by a backslash
    # and the symbol.  This looks like it doesn't do anything.  However,
    # the backslash acts as a quote symbol in the first instance of
    # sym and as a literal in the second instance.
    $str =~ s/$sym/$sym/g;
  } # foreach
  # That's it, we're done.  Return the modified string.
  return $str;
} # slashify


#This program will print what a SEARCH string sees of a variable;
#since PRINT & SEARCH have different special characters, this will
#ignore the \s in front of things that SEARCH needs to see literally,
#and marks things SEARCH will see as commands _clearly_.

#use of foreach command inspired by slashify, by Sam Rebelsky (see above)
sub searchsee {
  #Should only have 1 variable to play with..
  my ($str) = @_;
  #Here we'll get a list of "\Special"s, 
  #where Special = a special search character
  foreach $sym (split(//,"|()[{^$+?.")) {
    my ($unsym) = $sym;
    $sym = "\\".$sym;
    #we want to replace these \Specials with just their non-special value... 
    #This should make the search-special thing
    #look like it's non-special value 
    $str =~ s/([^\\]|^)($sym)/$1  BAD->_$2_  /g;
    #this line makes the appropriately-slashed ones slash-transparent
    $str =~ s/\\$sym/$unsym/g;
  }
  return $str;
}#searchsee



#This was modified from Sam Rebelsky's code by Rachel Heck
sub backslash {
  # Get the parameters
  my $str = shift;

  # Backslash the slashes first, so that if we insert any others,
  # they don't get slashified.
  $str =~ s/\\/\\\\/g;

  # The important symbols are listed on page 59 of the book.
  # For each such symbol

@things = ("\|","\(","\)","\[","\{","\^","\$","\*","\+","\?","\."); 
  foreach $sym (@things) {
    $sym = "\\" . $sym;
    # Replace all copies of the particular symbol by a backslash
    # and the symbol.  This looks like it doesn't do anything.  However,
    # the backslash acts as a quote symbol in the first instance of
    # sym and as a literal in the second instance.
    $str =~ s/$sym/$sym/g;
  } # foreach
  # That's it, we're done.  Return the modified string.
  return $str;
} # backslash

