#!/usr/bin/perl $begin_code_pat = "^\@begin code"; $end_code_pat = "^\@end code"; $code_line_pat = "^\@text"; $index_prefix = "\@index defn"; sub process_code_chunk { while ( ($_ = <>) && !/$end_code_pat/o ) { print $_; if( /$code_line_pat/o ) { $_ =~ s/#.*//o; @def_list = (/sub\s(\w+)/go, /package\s(\w+)/go); foreach $item (@def_list) { print "$index_prefix $item\n"; } } } # make sure we print the "@end code" line print $_; } while ( <> ) { print $_; if (/$begin_code_pat/o) { process_code_chunk; } }