#!/usr/bin/perl -i.bak -pw use strict; use vars '@Reg_list', '%Action', '%Match_table'; BEGIN { (my $basename = $0) =~ s!.*/!!; my $Comment = "%"; #Latex & matlab style. Change as you wish. #(I should make this user-modifiable, e.g. a #command-line arg...) if (@ARGV == 0 or $ARGV[0] eq '-h' or $ARGV[0] eq '--help') { print < [...] file [...] where ACTION is performed on each line in each file matching the regexp MATCH. Two notes: o Usually it is convenient to quote the entire expression to hide any shell characters in your regexp, e.g. 'COMMENT=%remove\$'. o Backslashes in your regexp (that you wish treated as literal backslashes) MUST be escaped. Unescaped backslashes will be interpreted by perl as regexp metacharacters. (Regexp metacharacters are _not_ escaped internally by this program, so the user can have as much proverbial rope as they want.) Current actions implemented are: COMMENT: adds comment character ("$Comment ") to beginning of line UNCOMMENT: removes comment characters from beginning of line USAGE exit; } # Developer notes: New actions can be added to the hash here. # Preferably the action should modify its single argument, returning # either that modified value or an error code (currently the return # value is discarded but I should probably do something more useful; # I was thinking in terms of functions like chomp when I wrote this, # but then got lazy). %Action = (COMMENT => sub {$_[0] = "$Comment $_[0]";}, UNCOMMENT => sub {$_[0] =~ s/^($Comment|\s)+//;}, ); while ($ARGV[0] =~ /=/) { my ($Command, $Regexp) = split(/=/, shift @ARGV, 2); exists($Action{$Command}) or die "$basename: Unrecognized action $Command from command line.\n"; push (@Reg_list, $Regexp); $Match_table{$Regexp} = $Command; } } foreach my $Regexp (@Reg_list) { if (/$Regexp/) { $Action{$Match_table{$Regexp}}->($_); } }