root/trunk/bin/extract-perl-from-mungo.pl

Revision 110, 2.8 kB (checked in by clinton, 4 years ago)

Update docs to include StatINC

  • Property svn:executable set to *
  • Property svn:keywords set to Revision
Line 
1 #!/opt/OMNIperl/bin/perl
2 use strict;
3 use warnings;
4
5 use Mungo;
6
7 use Getopt::Long;
8
9 #=============================================================================#
10 #                                  INIT
11 #=============================================================================#
12
13 my ($SVN_VERSION) = q$Revision$ =~ /(\d+)/;
14 my $VERSION_NUM = "1.0.0.${SVN_VERSION}";
15
16 my $APPEND_LINE_NUMBERS = 0;
17 my $APPEND_FILE_NAMES = 0;
18 my $HELP = 0;
19 my $USAGE = 0;
20 my $SHOW_VERSION = 0;
21
22 GetOptions(
23            'number-lines' => \$APPEND_LINE_NUMBERS,
24            'file-names'   => \$APPEND_FILE_NAMES,
25            'help'         => \$HELP,
26            'usage'        => \$USAGE,
27            'version'      => \$SHOW_VERSION,
28           ) || usage('', 1);
29 if ($HELP || $USAGE) { usage('', 0); }
30 if ($SHOW_VERSION) { show_version(); }
31
32 my @files = @ARGV;
33 unless (@files) {  usage('No files specified.', 2); }
34
35 #=============================================================================#
36 #                               PROCESSING
37 #=============================================================================#
38
39 foreach my $file (@files) {
40     my $mungo_string = slurp_file($file);
41     my $perl_string = Mungo::convertStringToExpression(\$mungo_string);
42     print $perl_string;
43 }
44
45 #=============================================================================#
46 #                              SUB SAMMICHES
47 #=============================================================================#
48
49 sub slurp_file {
50     my $file = shift;
51     unless (-e $file) {
52         die "No such file or directory '$file'\n";
53     }
54
55     open(MUNGOFILE, "<$file") or die "Could not open $file: $!";
56     my $line_num = 0;
57     my $mungo_content = '';
58     while (my $mungo_line = <MUNGOFILE>) {
59         if ($APPEND_LINE_NUMBERS || $APPEND_FILE_NAMES) {
60             $line_num++;
61             my $append = '#'
62               . ($APPEND_FILE_NAMES ? " file $file" : '')
63                 . ($APPEND_LINE_NUMBERS ? " line $line_num" : '')
64                   . "\n";
65             $mungo_line =~ s{\n$}{$append};
66         }
67         $mungo_content .= $mungo_line;
68     }
69     close MUNGOFILE;
70     return $mungo_content;
71 }
72
73 sub usage {
74     my $message = shift || '';
75     my $exit_code = shift || 0;
76
77     print <<EOT;
78 $message
79
80 $0 { --usage | --help }
81   Display this message.
82
83 $0 [--number-lines] [--file-names]
84    file1 [file2 ...]
85
86   Extract Perl code from the given mungo files and print to STDOUT.
87
88 Options:
89   --number-lines   Append a comment at the end of each line with the line
90                    number from the original file as '# line \\d+'
91   --file-names     Append a comment at the end of each line with the
92                    filename as '# file FILENAME'.
93
94 EOT
95     exit($exit_code);
96
97 }
98
99 sub show_version {
100     print "$0 version " . $VERSION_NUM . "\n";
101     exit(0);
102 }
Note: See TracBrowser for help on using the browser.