#!/usr/bin/perl

=head1 NAME
  ptags - generate perl tags file

=head1 SYNOPSIS
  ptags file... >tags

=head1 DESCRIPTION
  This scans the files for perl functions and produces a vi(1) "tags"
  file  on standard output.  This will make it possible for a vi user
  to use the -t option and the :ta command with per code.

  This program's output may be merged with other tags files, such  as
  those  created by ctags(1).  Note that the tags file must be sorted
  alphabetically to work properly.  So if you want to run both  ctags
  and  ptags,  and  merge  the results, you will need to do something
  like:

=begin text
  ctags -f tags_c *.[ch]
  ptags  > tags_p *.p[lm]
  cat tags_* | sort > tags
=end text

=head1 OPTIONS
  None, so far.

=head1 FILES
  This program can't act as a "filter" and read from  stdin,  because
  it  must  know the names of the files that it's scanning.  The file
  names must go into the output, after all, so we  must  insist  that
  you list them on the command line.  Sorry about that.

=head1 BUGS
  This program only recognizes "sub foo" if the two tokens are at the
  start of a line.

  We  should  probably add code to recognize a few things in addition
  to functions.  Stay tuned ...

=head1 SEE ALSO
  ctags(1)

=head1 AUTHOR
  John Chambers <jc@trillian.mit.edu>
=cut

for $f (@ARGV) {
	if (!open(F,$f)) {
		print STDERR "$0: Can't read \"$f\" [$!]\n";
		next;
	}
	for $l (<F>) {
		if ($l =~ /^\s*sub\s*(\w+)\b/) {
			$tag{$1} = "$1\t$f\t/$&/";
		}
	}
}

for $k (sort keys %tag) {
	print $tag{$k} . "\n";
}
