Don't initialise all your variables in a great bolus at the top of your code. You wipe out half the advantage of using strictures if you do so. Instead declare and initialise variables to have as small a scope as makes sense. Always initialise variables with their first value when they are declared unless the declaration must be outside a loop.
my $i = 0;
foreach (@ARGV) {
if ($i == 0) {
print "$_";
$i = 1;
}
else {
print ", $_";
}
}
is much better written:
print join ', ', @ARGV;
New acts and scenes are better managed by:
my $new_act;
my $new_scene;
while (<$playIn>) {
my $line = $_;
...
# Print the act heading if we haven't yet.
if ($new_act) {
print "$new_act\n";
$new_act = undef;
}
# Print the scene headin if we haven't yet.
if ($new_scene) {
print "$new_scene\n";
$new_scene = undef;
}
...
# Step 1.6: We should also print act headings.
elsif ($line =~ m/.*<h3>ACT \w+.*/) {
$new_act = $line;
}
# Step 1.7: We should also print scene headings.
elsif ($line =~ m/.*<h3>SCENE \w+.*/) {
$new_scene = $line;
}
}
Don't use manifest constants ("true" and "false") for boolean values. They are prone to error and obscure the flow of the code by adding code. if ($new_scene) is easy to read an understand - anything more is harder to read, understand and get right.
Use three parameter open and lexical file handles:
open my $playIn, '<', $mit_shakes or die "Could not open '$mit_shakes'
+: $!\n";
Note that your open (open PLAY, $mit_shakes || die "Could not open file: $!\n";) is broken. Replace || with or for it to work as you expect.
True laziness is hard work
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.