0: #!perl
1: use strict;
2:
3: unless(scalar(@ARGV)==2){
4: print "\n$0 strips comment lines beginning with # from perl code";
5: print "\n - lines that begin with #! aren't stripped\n - use with caution, may strip more than comments!\n";
6: print "\nusage: perl unc.pl infile outfile";
7: print "\n set outfile to \"display\" to just show result\n";
8: exit
9: }
10: my$infile = shift;
11: my$outfile = shift;
12:
13: open(IN,"< $infile") or die "\n $infile not found \n";
14: my@in = <IN>;
15: close(IN) or die "$!";
16:
17: my%code = ();
18: my($d,$c) = 0;
19: my$t=scalar(@in);
20: for(@in){
21: unless($_=~/^(\s+)?#[^!]/){
22: $code{$c}=$_;
23: $c++
24: }
25: else{$d++}
26: }
27: my@out = sort {$a <=> $b} keys %code;
28:
29: if($outfile eq 'display'){
30: for(@out){
31: print $code{$_}
32: }
33: &sum();
34: exit
35: }
36: open(OUT,"> $outfile") or die "\n $outfile write error \n";
37: for(@out){
38: print OUT $code{$_}
39: }
40: close(OUT) or die "$!";
41: &sum();
42: exit;
43:
44: sub sum{
45: print qq~\n $t lines read from $infile\n~;
46: print qq~ $d comment lines detected in $infile\n~;
47: print qq~ $c lines written to $outfile\n~;
48: }
In reply to strip perl comment lines by epoptai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |