<?php include('HEADER.php'); ?>

<h1>Perl Cheatsheet</h1>

<p>This covers Perl 5. Also see my <a href="http://paulgorman.org/cheatsheets/regular_expressions.html">regular expressions</a> cheatsheet.</p>

<ul>

    <li><a href="#modules">Modules</a></li>
    <li><a href="#arrays">Arrays</a></li>
    <li><a href="#hashes">Hashes</a></li>
    <li><a href="#loops">Conditionals and Loops</a></li>
    <li><a href="#files">Files</a></li>
    <li><a href="#subroutines">Subroutines</a></li>
    <li><a href="#math">Math</a></li>
    <li><a href="#magicvariables">Magic variables</a></li>
</ul>

<h3 id="comments">Comments</h3>

<pre class="prettyprint"># Comments start with a hash.</pre>

<h3 id="strings">Strings</h3>

<pre class="prettyprint">$x = "A string";
print $x . ' ' . ' concatenated with dots.';</pre>

<h3 id="arrays">Arrays</h3>

<p>Initialize (clear) an array:</p>

<pre class="prettyprint">my @array = ();</pre>

<p>Assign values to an array:</p>

<pre class="prettyprint">@array = ("foo", "bar", "bat");</pre>

<p>Get value of an array element:</p>

<pre class="prettyprint">print $array[1];</pre>

<p>Pushing and popping elements to or from the end of an array:</p>

<pre class="prettyprint">push (@array, $newElement);
$x = pop (@array);</pre>

<p>The functions shift() and unshift() do the same thing as push() and pop(), but to the beginning of an array.</p>

<p>Find length of array:</p>

<pre class="prettyprint">$arraySize = scalar (@array);</pre>

<p>or:</p>

<pre class="prettyprint">$arraySize = $#array + 1;</pre>

<p>Arrays can be sorted in a variety of ways:</p>

<pre class="prettyprint">@reversedArray = reverse (@array);
@sortedAscendingArray = sort {$a <=> $b} @array;
@sortedDescendingArray = sort {$b <=> $a} @array;</pre>

<h3 id="hashes">Hashes</h3>

<p>Create a hash:</p>

<pre class="prettyprint">my %hash = ();
%hash = (
    key0    =&gt;   "value foo",
    key1    =&gt;   "value bar",
    key2    =&gt;   "value bat"
);</pre>

<p>Use hash:</p>

<pre class="prettyprint">print $hash{key1};</pre>

<p>Check if hash element exists:</p>

<pre class="prettyprint">if (exists ($hash{'key1'})) { print "Hash key1 exists."; }</pre>

<p>Add to a hash:</p>

<pre class="prettyprint">$hash{$key3} = "value bam";</pre>

<p>Remove hash element:</p>

<pre class="prettyprint">delete ($hash{'key3'});</pre>

<p>Iterate over a hash:</p>

<pre class="prettyprint">while (($key, $value) = each (%hash)) { print "$key and $value.\n"; }</pre>

<p>Another couple of ways to loop over hashes:</p>

<pre class="prettyprint">foreach $key (keys %hash) { print $hash{$key}; }
foreach $value (values %hash) { print $value; }</pre>

<h3 id="loops">Conditionals and Loops</h3>

<p><b>If/else</b> conditionals:</p>

<pre class="prettyprint">if ($n == 1) {
    print "It's 1.\n";
} elsif ($n == 2) {
    print "It's 2.\n";
} else {
    print "It's not 1 or 2.\n";
}</pre>

<p><b>for</b> loop:</p>

<pre class="prettyprint">for ($i = 0; $i &lt; 10; $i ++) {
    print "Count is $i.\n";
}</pre>

<p>The <b>foreach</b> loop, looping over elements in an array:</p>

<pre class="prettyprint"> foreach $element (@array) {
    print $element;
}
</pre>

<h3 id="files">Files</h3>

<p>Open a file, and read it:</p>

<pre class="prettyprint">open (INPUT, "&lt;$inputFile")
    or die ("Can't open read input file $inputFile");
while (&lt;INPUT&gt;) {
    print $_;
}
close (INPUT);</pre>

<p>Open a file, and write to it:</p>

<pre class="prettyprint">open (OUTPUT, "&gt;$outputFile")
  or die ("Can't open output file $outputFile");
print OUTPUT $output;
close (OUTPUT);</pre>

<h3 id="subroutines">Subroutines</h3>

<pre class="prettyprint">sub mysub {
    print "First subroutine argument is $_[0]\n";
}</pre>

<h3 id="modules">Modules</h3>

<p>Check if a module is installed by trying to open its documentation:</p>

<pre class="prettyprint">perldoc GD::Graph</pre>

<p>Install modules using the CPAN shell:</p>

<pre class="prettyprint">perl -MCPAN -e shell</pre>

<p>Find a module in CPAN:</p>

<pre class="prettyprint">cpan&gt; i /GD/</pre>

<p>Install a module using CPAN:</p>

<pre class="prettyprint">cpan&gt; install GD::Graph</pre>


<h3 id="math">Math</h3>

<p>Round to two decimal places:</p>

<pre class="prettyprint">$rounded = sprintf ("%.2f", 100.23456);</pre>

<h3 id="magicvariables">Magic variables</h3>

<p>This list is not exhaustive, but these are the ones I commonly use:</p>

<table>
    <tr><td>$_</td><td>Current default value</td></tr>
    <tr><td>$!</td><td>Current error</td></tr>
    <tr><td>@ARGV</td><td>Command line arguments</td></tr>
    <tr><td>@_</td><td>Subroutine arguments $_[0], $_[1], etc.</td></tr>
    <tr><td>$1</td><td>$2, $3, etc.; Pattern matches</td></tr>
    <tr><td>$&mp;</td><td>Last pattern match</td></tr>
    <tr><td>$.</td><td>Current input line number</td></tr>
    <tr><td></td><td></td></tr>
</table>

<?php include('../FOOTER.php'); ?>
