I use a homeboy data base technique to keep info about the scripts I
write and other typse of stuff too.  Here I'm just dealing with
scripts.

Its a simple format to enter key information about what a script
does.  Looks like:

# Keywords: SOME WORDS
# body
# body
# DATE
# &&

I've written various scripts to search this format in awk and shell.
Now trying it in perl.  I have several working scripts but wanted to
get some ideas from the sharp shooters here how to do this better.

My technique seems like it could be streamlined and improved quite a
lot.

The sample below just handles the basic technique and isn't completed
with all tests and etc.  Just some basic ones. But really I'm more
interested in hearing better ways to accomplish this.

The basic task is to locate a formated segment, search its keywords
line for regex then print the segment.  Also a basic check for
misformatted segments.

Not too concerned with how the files are aquired but what comes after.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#!/usr/local/bin/perl -w

($myscript = $0) =~ s:^.*/::; 
$regex = shift;
## Set Keywords start end regex for non script searching (The default)
$keyreg = '^# Keywords:';
$keyend = '^# &&$';

if (!$ARGV[0]) {
  usage();
  exit;
}
## Aquire there files in whatever way
@files = @ARGV;

## Set a marker to know when we are in a new file
$fname_for_line_cnt = '';
for (@files) { 
  chomp;
  $file = $_;
  if ("$fname_for_line_cnt" eq "$file") {
   ## This shouldn't happen
    print "We're reading the same file again .. exiting\n";
    exit;
  } else {
    ## Set lineno to 0 for start of each file
    $lineno = 0;
    $fname_for_line_cnt = $file;
  }

  if (-f $file) {
    open(FH,"<$file") or die "Cannot open $file: $!";
    while (<FH>) {
      chomp;
      $lineno++;
      $line = $_;
      if (/$keyreg $regex/) {
        print "$file\n";
        $hit = "TRUE";
      }
      if ($hit) {
        print "$lineno $line\n";
      }
      if ($hit && /$keyend/) {
      ## We've hit the end of a good segment, print delimiter and null out our vars
        print "-- \n";
        $hit        = '';
        $line       = '';
      }
      if ($hit && /^[^#]/ || $hit && eof) {
        ## If we see this situation it means the format is screwed up
        ## Notify user of the line number, but null out vars and proceed. 
        print  "$file:\n   INCOMPLETE SEGMENT ENTRY: Line <$lineno>\n --\n";
        $hit        = '';
        $line       = '';
      }
    }
    close(FH);
  } else {
    next;
  }
}
sub usage {
  print<<EOM;

Purpose: Search scripts keyword segments (or any file)
Usage: \`$myscript "REGEX" file ... fileN (or glob)'
      (Where REGEX is a regex to be found in Keyword segment) 

EOM
}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to