<?xml version="1.0" encoding="windows-1252"?>
<node id="1009248" title="Re: Making Cue Scripts with Perl" created="2012-12-17 17:14:41" updated="2012-12-17 17:14:41">
<type id="11">
note</type>
<author id="461912">
GrandFather</author>
<data>
<field name="doctext">
&lt;p&gt;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.&lt;/p&gt;
&lt;c&gt;
my $i = 0;

foreach (@ARGV) {
  if ($i == 0) {
    print "$_";
    $i = 1;
  }

  else {
    print ", $_";
  }
}
&lt;/c&gt;
&lt;p&gt;is much better written:&lt;/p&gt;
&lt;c&gt;
print join ', ', @ARGV;
&lt;/c&gt;
&lt;p&gt;New acts and scenes are better managed by:&lt;/p&gt;
&lt;c&gt;
my $new_act;
my $new_scene;

while (&lt;$playIn&gt;) {
    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/.*&lt;h3&gt;ACT \w+.*/) {
            $new_act = $line;
        }

        # Step 1.7: We should also print scene headings.
        elsif ($line =~ m/.*&lt;h3&gt;SCENE \w+.*/) {
            $new_scene = $line;
        }
    }
&lt;/c&gt;
&lt;p&gt;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. &lt;c&gt;if ($new_scene)&lt;/c&gt; is easy to read an understand - anything more is harder to read,  understand and get right.&lt;/p&gt;
&lt;p&gt;Use three parameter [doc://open] and lexical file handles:&lt;/p&gt;
&lt;c&gt;
open my $playIn, '&lt;', $mit_shakes or die "Could not open '$mit_shakes': $!\n";
&lt;/c&gt;
&lt;p&gt;Note that your open (&lt;c&gt;open PLAY, $mit_shakes || die "Could not open file: $!\n";&lt;/c&gt;) is broken. Replace &lt;c&gt;||&lt;/c&gt; with &lt;c&gt;or&lt;/c&gt; for it to work as you expect.&lt;/p&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-461912"&gt;
True laziness is hard work
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1009211</field>
<field name="parent_node">
1009211</field>
</data>
</node>
