I have now used this structure, and I'm curious what your take on the approach is - either if it changes your opinion of the structure or if you have a more aesthetically pleasing alternative. I'm writing what is essentially a preprocessor for some code autotranslation, and so I have a volatile content string that I process through a series of
if (){next} blocks. I then check for the error condition in the continue block, which prevents the possibility of infinite loops and cleans up error handling. I've just started the project, but expect the number of conditional clauses to grow significantly and this strikes me as the most extensible framework.
#!/usr/bin/perl
use strict;
use warnings;
local $/; # slurp
my $content = <DATA>;
my $error;
# Preprocess
while (local ($_) = $content =~ /^#include\s(.*?)$/m) {
if (/<math/) { # Math library
$content =~ s/#include\s$_/import java.lang.Math;/;
next;
}
if (/"stdafx.h"/) { # Autogen MS IDE header for project/system inc
+ludes
$content =~ s/#include\s$_//;
next;
}
if (/"/) { # A yet unconsidered header file
(my $filename) = /"(.*)"/;
open my $inc_handle, '<', $filename or warn "File open fail $f
+ilename: $!\n" and next;
local $/; # slurp
my $include = <$inc_handle>;
$content =~ s/#include\s$_/$include/;
next;
}
} continue {
if ($content =~ s/#include\s$_//) {
warn "Unhandled include $_\n";
$error++;
}
}
die "$error errors encountered" if $error;
__DATA__
#include "stdafx.h"
#include <math.h>
#include "AlyLee.h"
#include "Common.h"
Note that while lexical variables are defined at the block level (as
AnomalousMonk pointed out), the localization in the while conditional holds through the continue block.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.