Commits

Anonymous committed 0f7d45d

Updated the Perl-for-Newbies presentations.

Comments (0)

Files changed (6)

lib/tutorials/perl-for-newbies/lect2-all-in-one/argv/copy.pl

 my $filename = $ARGV[0];
 
 open my $in, "<", $filename;
-open $out, ">", $filename.".bak";
+open my $out, ">", $filename.".bak";
 print {$out} join("",<$in>);
 close($in);
 close($out);

lib/tutorials/perl-for-newbies/lect2-all-in-one/files/print2.pl

 use strict;
 use warnings;
 
-open $seq1, ">", "seq1.txt";
-open $seq2, ">", "seq2.txt";
+open my $seq1, ">", "seq1.txt";
+open my $seq2, ">", "seq2.txt";
 
 for($a=0;$a<100;$a++)
 {

lib/tutorials/perl-for-newbies/lect2-all-in-one/index.html

 
 <span class="Statement">my</span> <span class="Identifier">$pyramid_side</span> = <span class="Constant">20</span>;
 
-<span class="Statement">open</span> <span class="Identifier">O</span>, <span class="Constant">&quot;&gt;&quot;</span>, <span class="Constant">&quot;pyramid.txt&quot;</span>;
+<span class="Statement">open</span> <span class="Statement">my</span> <span class="Identifier">$out</span>, <span class="Constant">&quot;&gt;&quot;</span>, <span class="Constant">&quot;pyramid.txt&quot;</span>;
 <span class="Statement">for</span>(<span class="Identifier">$a</span>=<span class="Constant">1</span> ; <span class="Identifier">$a</span> &lt;= <span class="Identifier">$pyramid_side</span> ; <span class="Identifier">$a</span>++)
 {
-    <span class="Statement">print</span> <span class="Identifier">O</span> <span class="Constant">&quot;X&quot;</span> x <span class="Identifier">$a</span>;
-    <span class="Statement">print</span> <span class="Identifier">O</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
+    <span class="Statement">print {</span><span class="Identifier">$out</span><span class="Statement">}</span> <span class="Constant">&quot;X&quot;</span> x <span class="Identifier">$a</span>;
+    <span class="Statement">print {</span><span class="Identifier">$out</span><span class="Statement">}</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
 }
-<span class="Statement">close</span>(<span class="Identifier">O</span>);
+<span class="Statement">close</span>(<span class="Identifier">$out</span>);
 </pre></div>
 <p>
 In order to print to more than one file at once, one needs to use two
 <span class="Statement">use strict</span>;
 <span class="Statement">use warnings</span>;
 
-<span class="Statement">open</span> <span class="Identifier">SEQ1</span>, <span class="Constant">&quot;&gt;&quot;</span>, <span class="Constant">&quot;seq1.txt&quot;</span>;
-<span class="Statement">open</span> <span class="Identifier">SEQ2</span>, <span class="Constant">&quot;&gt;&quot;</span>, <span class="Constant">&quot;seq2.txt&quot;</span>;
+<span class="Statement">open</span> <span class="Statement">my</span> <span class="Identifier">$seq1</span>, <span class="Constant">&quot;&gt;&quot;</span>, <span class="Constant">&quot;seq1.txt&quot;</span>;
+<span class="Statement">open</span> <span class="Statement">my</span> <span class="Identifier">$seq2</span>, <span class="Constant">&quot;&gt;&quot;</span>, <span class="Constant">&quot;seq2.txt&quot;</span>;
 
 <span class="Statement">for</span>(<span class="Identifier">$a</span>=<span class="Constant">0</span>;<span class="Identifier">$a</span>&lt;<span class="Constant">100</span>;<span class="Identifier">$a</span>++)
 {
-    <span class="Statement">print</span> <span class="Identifier">SEQ1</span> <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
-    <span class="Statement">print</span> <span class="Identifier">SEQ2</span> <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
-    <span class="Statement">print</span> <span class="Identifier">SEQ1</span> (<span class="Identifier">$a</span>+<span class="Constant">0.1</span>);
-    <span class="Statement">print</span> <span class="Identifier">SEQ2</span> (<span class="Identifier">$a</span>+<span class="Constant">0.5</span>);
-    <span class="Statement">print</span> <span class="Identifier">SEQ1</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
-    <span class="Statement">print</span> <span class="Identifier">SEQ2</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
+    <span class="Statement">print {</span><span class="Identifier">$seq1</span><span class="Statement">}</span> <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
+    <span class="Statement">print {</span><span class="Identifier">$seq2</span><span class="Statement">}</span> <span class="Identifier">$a</span>, <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
+    <span class="Statement">print {</span><span class="Identifier">$seq1</span><span class="Statement">}</span> (<span class="Identifier">$a</span>+<span class="Constant">0.1</span>);
+    <span class="Statement">print {</span><span class="Identifier">$seq2</span><span class="Statement">}</span> (<span class="Identifier">$a</span>+<span class="Constant">0.5</span>);
+    <span class="Statement">print {</span><span class="Identifier">$seq1</span><span class="Statement">}</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
+    <span class="Statement">print {</span><span class="Identifier">$seq2</span><span class="Statement">}</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
 }
 
-<span class="Statement">close</span>(<span class="Identifier">SEQ1</span>);
-<span class="Statement">close</span>(<span class="Identifier">SEQ2</span>);
+<span class="Statement">close</span>(<span class="Identifier">$seq1</span>);
+<span class="Statement">close</span>(<span class="Identifier">$seq2</span>);
 </pre></div>
 <hr />
 
 <span class="Statement">my</span> (<span class="Identifier">$line_num</span>, <span class="Identifier">$line</span>);
 
 <span class="Identifier">$line_num</span> = <span class="Constant">0</span>;
-<span class="Statement">open</span> <span class="Identifier">I</span>, <span class="Constant">&quot;&lt;&quot;</span>, <span class="Constant">&quot;input.txt&quot;</span>;
-<span class="Statement">open</span> <span class="Identifier">O</span>, <span class="Constant">&quot;&gt;&quot;</span>, <span class="Constant">&quot;output.txt&quot;</span>;
+<span class="Statement">open</span> <span class="Statement">my</span> <span class="Identifier">$in</span>, <span class="Constant">&quot;&lt;&quot;</span>, <span class="Constant">&quot;input.txt&quot;</span>;
+<span class="Statement">open</span> <span class="Statement">my</span> <span class="Identifier">$out</span>, <span class="Constant">&quot;&gt;&quot;</span>, <span class="Constant">&quot;output.txt&quot;</span>;
 
-<span class="Statement">while</span> (<span class="Identifier">$line</span> = <span class="Identifier">&lt;I&gt;</span>)
+<span class="Statement">while</span> (<span class="Identifier">$line</span> = &lt;<span class="Identifier">$in</span>&gt;)
 {
     <span class="Comment"># We aren't chomping it so we won't lose the newline.</span>
-    <span class="Statement">print</span> <span class="Identifier">O</span> <span class="Identifier">$line_num</span>, <span class="Constant">&quot;: &quot;</span>, <span class="Identifier">$line</span>;
+    <span class="Statement">print {</span><span class="Identifier">$out</span><span class="Statement">}</span> <span class="Identifier">$line_num</span>, <span class="Constant">&quot;: &quot;</span>, <span class="Identifier">$line</span>;
     <span class="Identifier">$line_num</span>++;
 }
 
-<span class="Statement">close</span> (<span class="Identifier">I</span>);
-<span class="Statement">close</span> (<span class="Identifier">O</span>);
+<span class="Statement">close</span> (<span class="Identifier">$in</span>);
+<span class="Statement">close</span> (<span class="Identifier">$out</span>);
 </pre></div>
 <p>
 And the following example counts the number of lines in a file that start with
 
 <span class="Identifier">$lines_num</span> = <span class="Constant">0</span>;
 <span class="Identifier">$filename</span> = <span class="Constant">&quot;input.txt&quot;</span>;
-<span class="Statement">open</span> <span class="Identifier">I</span>,  <span class="Constant">&quot;&lt;&quot;</span>, <span class="Identifier">$filename</span>;
-<span class="Statement">while</span> (<span class="Identifier">$line</span> = <span class="Identifier">&lt;I&gt;</span>)
+<span class="Statement">open</span> <span class="Statement">my</span> <span class="Identifier">$in</span>,  <span class="Constant">&quot;&lt;&quot;</span>, <span class="Identifier">$filename</span>;
+<span class="Statement">while</span> (<span class="Identifier">$line</span> = &lt;<span class="Identifier">$in</span>&gt;)
 {
     <span class="Identifier">$c</span> = <span class="Statement">substr</span>(<span class="Identifier">$line</span>, <span class="Constant">0</span>, <span class="Constant">1</span>);
     <span class="Statement">if</span> (<span class="Statement">lc</span>(<span class="Identifier">$c</span>) <span class="Statement">eq</span> <span class="Constant">&quot;a&quot;</span>)
         <span class="Identifier">$lines_num</span>++;
     }
 }
-<span class="Statement">close</span>(<span class="Identifier">I</span>);
+<span class="Statement">close</span>(<span class="Identifier">$in</span>);
 
 <span class="Statement">print</span> <span class="Constant">&quot;In &quot;</span> , <span class="Identifier">$filename</span>, <span class="Constant">&quot; there are &quot;</span>,
     <span class="Identifier">$lines_num</span>, <span class="Constant">&quot; lines that start with </span><span class="Special">\&quot;</span><span class="Constant">A</span><span class="Special">\&quot;</span><span class="Constant">.</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
 
 <span class="Statement">my</span> <span class="Identifier">$filename</span> = <span class="Identifier">$ARGV[</span><span class="Constant">0</span><span class="Identifier">]</span>;
 
-<span class="Statement">open</span> <span class="Identifier">I</span>, <span class="Constant">&quot;&lt;&quot;</span>, <span class="Identifier">$filename</span>;
-<span class="Statement">open</span> <span class="Identifier">O</span>, <span class="Constant">&quot;&gt;&quot;</span>, <span class="Identifier">$filename</span>.<span class="Constant">&quot;.bak&quot;</span>;
-<span class="Statement">print</span> <span class="Identifier">O</span> <span class="Statement">join</span>(<span class="Constant">&quot;&quot;</span>,<span class="Identifier">&lt;I&gt;</span>);
-<span class="Statement">close</span>(<span class="Identifier">I</span>);
-<span class="Statement">close</span>(<span class="Identifier">O</span>);
+<span class="Statement">open</span> <span class="Statement">my</span> <span class="Identifier">$in</span>, <span class="Constant">&quot;&lt;&quot;</span>, <span class="Identifier">$filename</span>;
+<span class="Statement">open</span> <span class="Statement">my</span> <span class="Identifier">$out</span>, <span class="Constant">&quot;&gt;&quot;</span>, <span class="Identifier">$filename</span>.<span class="Constant">&quot;.bak&quot;</span>;
+<span class="Statement">print {</span><span class="Identifier">$out</span><span class="Statement">}</span> <span class="Statement">join</span>(<span class="Constant">&quot;&quot;</span>,&lt;<span class="Identifier">$in</span>&gt;);
+<span class="Statement">close</span>(<span class="Identifier">$in</span>);
+<span class="Statement">close</span>(<span class="Identifier">$out</span>);
 </pre></div>
 <p>
 Using the command-line for specifying parameters to the program is usually
 
 <span class="Statement">my</span> <span class="Identifier">$user_name</span> = <span class="Statement">shift</span>;
 
-<span class="Statement">open</span> <span class="Identifier">I</span>, <span class="Constant">&quot;&lt;&quot;</span>, <span class="Constant">&quot;/etc/passwd&quot;</span>;
-<span class="Statement">while</span> (<span class="Identifier">$line</span> = <span class="Identifier">&lt;I&gt;</span>)
+<span class="Statement">open</span> <span class="Statement">my</span> <span class="Identifier">$in</span>, <span class="Constant">&quot;&lt;&quot;</span>, <span class="Constant">&quot;/etc/passwd&quot;</span>;
+<span class="Statement">while</span> (<span class="Identifier">$line</span> = &lt;<span class="Identifier">$in</span>&gt;)
 {
     <span class="Identifier">@parts</span> = <span class="Statement">split</span>(<span class="Statement">/</span><span class="Constant">:</span><span class="Statement">/</span>, <span class="Identifier">$line</span>);
     <span class="Statement">if</span> (<span class="Identifier">$parts[</span><span class="Constant">0</span><span class="Identifier">]</span> <span class="Statement">eq</span> <span class="Identifier">$user_name</span>)
         <span class="Statement">exit</span>(<span class="Constant">0</span>);
     }
 }
-<span class="Statement">close</span>(<span class="Identifier">I</span>);
+<span class="Statement">close</span>(<span class="Identifier">$in</span>);
 
 <span class="Statement">print</span> <span class="Identifier">$user_name</span> . <span class="Constant">&quot;'s user ID was not found!</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
 <span class="Statement">exit</span>(-<span class="Constant">1</span>);
 
 <span class="Statement">my</span> <span class="Identifier">$filename</span> = <span class="Statement">shift</span>;
 
-<span class="Statement">open</span> <span class="Identifier">I</span>, <span class="Constant">&quot;&lt;&quot;</span>, <span class="Identifier">$filename</span>;
-<span class="Statement">while</span> (<span class="Identifier">$l</span> = <span class="Identifier">&lt;I&gt;</span>)
+<span class="Statement">open</span> <span class="Statement">my</span> <span class="Identifier">$in</span>, <span class="Constant">&quot;&lt;&quot;</span>, <span class="Identifier">$filename</span>;
+<span class="Statement">while</span> (<span class="Identifier">$l</span> = &lt;<span class="Identifier">$in</span>&gt;)
 {
     <span class="Statement">chomp</span>(<span class="Identifier">$l</span>);
     <span class="Statement">push</span> <span class="Identifier">@lines</span>, <span class="Identifier">$l</span>;
 }
-<span class="Statement">close</span>(<span class="Identifier">I</span>);
+<span class="Statement">close</span>(<span class="Identifier">$in</span>);
 
 <span class="Comment"># Filter the comments</span>
 <span class="Statement">my</span> <span class="Identifier">@comments</span> = <span class="Statement">grep</span>(<span class="Statement">/</span><span class="Constant">^#</span><span class="Statement">/</span>, <span class="Identifier">@lines</span>);

lib/tutorials/perl-for-newbies/lect2-all-in-one/useful_funcs/grep.pl

     chomp($l);
     push @lines, $l;
 }
-close(I);
+close($in);
 
 # Filter the comments
 my @comments = grep(/^#/, @lines);

lib/tutorials/perl-for-newbies/lect4-all-in-one/index.html

 <span class="Statement">use strict</span>;
 <span class="Statement">use warnings</span>;
 
-<span class="Statement">open</span> <span class="Identifier">I</span>, <span class="Constant">&quot;/sbin/ifconfig |&quot;</span>;
-<span class="Statement">my</span> (<span class="Identifier">$line</span>, <span class="Identifier">@addrs</span>);
-<span class="Statement">while</span> (<span class="Identifier">$line</span> = <span class="Identifier">&lt;I&gt;</span>)
+<span class="Statement">open</span> <span class="Statement">my</span> <span class="Identifier">$in</span>, <span class="Constant">&quot;/sbin/ifconfig |&quot;</span>;
+
+<span class="Statement">my</span> (<span class="Identifier">@addrs</span>);
+
+<span class="Statement">while</span> (<span class="Statement">my</span> <span class="Identifier">$line</span> = &lt;<span class="Identifier">$in</span>&gt;)
 {
     <span class="Statement">if</span> (<span class="Identifier">$line</span> =~ <span class="Statement">/</span><span class="Constant">inet addr:</span><span class="Special">((\d+\.)+\d)</span><span class="Statement">/</span>)
     {
         <span class="Statement">push</span> <span class="Identifier">@addrs</span>, <span class="Identifier">$1</span>;
     }
 }
-<span class="Statement">close</span>(<span class="Identifier">I</span>);
+<span class="Statement">close</span>(<span class="Identifier">$in</span>);
+
 <span class="Statement">print</span> <span class="Constant">&quot;You have the following addresses: </span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Statement">join</span>(<span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>,<span class="Identifier">@addrs</span>), <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">&quot;</span>;
 </pre></div>
 <hr />

lib/tutorials/perl-for-newbies/lect4-all-in-one/processes/open1.pl

 use strict;
 use warnings;
 
-open I, "/sbin/ifconfig |";
-my ($line, @addrs);
-while ($line = <I>)
+open my $in, "/sbin/ifconfig |";
+
+my (@addrs);
+
+while (my $line = <$in>)
 {
     if ($line =~ /inet addr:((\d+\.)+\d)/)
     {
         push @addrs, $1;
     }
 }
-close(I);
+close($in);
+
 print "You have the following addresses: \n", join("\n",@addrs), "\n";