<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Learn Perl</title>
	<atom:link href="/feed/" rel="self" type="application/rss+xml" />
	<link>https://learnperl.org</link>
	<description>Learning Perl is fun</description>
	<lastBuildDate>Sun, 21 Oct 2012 03:48:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>Lesson 06 &#8211; Functions</title>
		<link>https://learnperl.org/lesson-06-functions/</link>
		<comments>https://learnperl.org/lesson-06-functions/#comments</comments>
		<pubDate>Tue, 16 Oct 2012 03:46:01 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://learnperl.org/?p=124</guid>
		<description><![CDATA[Functions are used when you want to reuse a block of code. You call or invoke a function by its name and passing arguments if the functions accepts any. In Perl, @_ contains a list of arguments that was passed &#8230; <a href="/lesson-06-functions/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Functions are used when you want to reuse a block of code. You call or invoke a function by its name and passing arguments if the functions accepts any.</p>
<p>In Perl, @_ contains a list of arguments that was passed to the function. This list will have scalars, arrays and hashes flattened into one.</p>
<p>The example below will show how to call a function and define a function. Note that only a scalar was passed to the function and $_[0] contains that value;</p>
<pre>#!/usr/bin/perl

use strict;
use warnings;

my $price = 12.99;

calculate_total($price);

sub calculate_total
{
    my $p = $_[0];

    print "You must pay ", $p;
}</pre>
<p>When you run the following script, Perl will give you a warning that says &#8220;Odd number of elements in hash assignment&#8221; and $d is undef. This is because the hash is greedy and gets all the values from @_.</p>
<pre>#!/usr/bin/perl

use strict;
use warnings;

my %pizza = (plain =&gt; 9.99, pepperoni =&gt; 10.99);
my $drink = 3.99;

display_price(%pizza, $drink);

sub display_price
{
    my (%p, $d) = @_;

    foreach my $i (keys %p)
    {
        print $p{$i}, "\n";
    }
    print $d, "\n";
}</pre>
<p>Instead, you will want to pass the scalar first and then the hash. This will produce what you expected.</p>
<pre>#!/usr/bin/perl

use strict;
use warnings;

my %pizza = (plain =&gt; 9.99, pepperoni =&gt; 10.99);
my $drink = 3.99;

display_price($drink, %pizza);

sub display_price
{
    my ($d, %p) = @_;

    foreach my $i (keys %p)
    {
        print $p{$i}, "\n";
    }
    print $d, "\n";
}</pre>
<p>This example demonstrates how hash references are passed to functions.</p>
<pre>#!/usr/bin/perl

use strict;
use warnings;

my %pizza = ( plain =&gt; { medium =&gt; 7.99, large =&gt; 9.99}, 
              pepperoni =&gt; { medium =&gt; 9.99, large =&gt; 10.99}, 
              supreme =&gt; { medium =&gt; 10.99, large =&gt; 12.99}, 
              veggie =&gt; { medium =&gt; 9.99, large =&gt; 10.99}
            );
my %order = ( plain =&gt; { large =&gt; 2},
              pepperoni =&gt; { large =&gt; 2}
            );

calculate_total(\%pizza, \%order);

sub calculate_total
{
    my ($p, $o) = @_;

    my $total = 0;
    foreach my $style (keys %$o) {
        print "$style \n";
        foreach my $size (keys %{ $o-&gt;{$style} }) {
            my $pies = $o-&gt;{$style}{$size};
            print "$size: $pies pies\n";

            $total += ($p-&gt;{$style}{$size} * $pies);
        }
    }
    print "Your total is ", $total, "\n";
}</pre>
<p>If you want to return $total to the main, you will have to use a return statement inside the function.</p>
<pre>
return $total;
</pre>
]]></content:encoded>
			<wfw:commentRss>https://learnperl.org/lesson-06-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lesson 05 &#8211; References</title>
		<link>https://learnperl.org/lesson-05-references/</link>
		<comments>https://learnperl.org/lesson-05-references/#comments</comments>
		<pubDate>Mon, 15 Oct 2012 02:37:23 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://learnperl.org/?p=94</guid>
		<description><![CDATA[In Perl, there is no such things as array of arrays, array of hashes, hash of arrays and hash of hashes. Perl has no values that are arrays or hashes. $president{'name'} = ('Clinton', 'Bush', 'Obama'); This is a scalar assignment &#8230; <a href="/lesson-05-references/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In Perl, there is no such things as array of arrays, array of hashes, hash of arrays and hash of hashes. Perl has no values that are arrays or hashes.</p>
<p><code>$president{'name'} = ('Clinton', 'Bush', 'Obama');</code></p>
<p>This is a scalar assignment (because you’re assigning to a scalar variable). That means the right-hand side will be evaluated in scalar context.</p>
<p>in scalar context, the comma operator evaluates its left operand, throws the result away, then returns the right operand.</p>
<p><code>$x = (1, 2);</code></p>
<p>In this line, 1, 2 will evaluate 1, ignore it, then return 2 and you’ll get a warning for having a constant in void context.</p>
<p>“You can’t have a hash whose values are arrays; hash values can only be scalars.” – from <a href="http://perldoc.perl.org/perlreftut.html">perlreftut</a></p>
<p>In Perl, “value” is synonymous with “scalar value”. Perl is somewhat unique with its containers/values duality. You have containers like scalar variables, arrays, and hashes and then you have values like undef, numbers, strings, references.</p>
<p>The main things to keep in mind are the behaviors of arrays, hashes, and the comma operator in scalar/list context. In list context it evaluates both of its operands in list context, then concatenates them. Or shorter: , is list concatenation. An array in scalar context yields the number of its elements. An array in list context yields a list of its elements. A hash in scalar context yields a string (this is somewhat obscure). the main thing to keep in mind is that for an empty hash that string will be “” (false), and for a non-empty hash it will be true.</p>
<p>In Perl, when we use an array with a reference to another array, that reference is considered a scalar.</p>
<address>In Perl, an array is a container and a list is … multiple values.</address>
<address>The following example uses references to build a nested data structure. It is an anonymous hash inside a hash. Notice how it uses curly braces for the anonymous hash instead of parentheses. (Square brackets would be used if it was an anonymous array.)</address>
<pre>#!/usr/bin/perl

use strict;
use warnings;

my %pizza = (
              "plain" =&gt; {"medium" =&gt; 7.99, "large" =&gt; 9.99}, 
              "pepperoni" =&gt; {"medium" =&gt; 9.99, "large" =&gt; 10.99}, 
              "supreme" =&gt; {"medium" =&gt; 10.99, "large" =&gt; 12.99}, 
              "veggie" =&gt; {"medium" =&gt; 9.99, "large" =&gt; 10.99}
            );

print "A large pepperoni pizza costs $", $pizza{pepperoni}{large}, "\n";</pre>
<p>Of course, you can build the same nested data structure by explicitly assigning a hashref to a scalar variable and using it inside a hash.</p>
<pre>my %plain_pricing = ("medium" =&gt; 7.99, "large" =&gt; 9.99);
my $plain = \%plain_pricing;
my %pepperoni_pricing = ("medium" =&gt; 9.99, "large" =&gt; 10.99);
my $pepperoni = \%pepperoni_pricing;
my %supreme_pricing = ("medium" =&gt; 10.99, "large" =&gt; 12.99);
my $supreme = \%supreme_pricing;
my %veggie_pricing = ("medium" =&gt; 9.99, "large" =&gt; 10.99);
my $veggie = \%veggie_pricing;
my %pizza2 = (
               "plain" =&gt; $plain, 
               "pepperoni" =&gt; $pepperoni, 
               "supreme" =&gt; $supreme, 
               "veggie" =&gt; $veggie
             );

print "A large pepperoni pizza costs $", $pizza2{pepperoni}{large}, "\n";</pre>
<p>You can also assign a value directly to the nested data structure and Perl will automatically create the reference.</p>
<pre>my %pizza3;

$pizza3{pepperoni}{large} = 10.99;
print "A large pepperoni pizza costs $", $pizza3{pepperoni}{large}, "\n";</pre>
<p>If you want to explicitly destroy a reference, you write undef $ref;</p>
<p>Normally there is no need to manually clear references. They get destroyed at the end of whatever scope they are used in.</p>
<p>Think of it as &#8220;once the variable is no longer needed, Perl undef()s it for you.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>https://learnperl.org/lesson-05-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lesson 04 &#8211; Regular Expressions</title>
		<link>https://learnperl.org/lesson-04-regular-expressions/</link>
		<comments>https://learnperl.org/lesson-04-regular-expressions/#comments</comments>
		<pubDate>Sun, 30 Sep 2012 23:35:47 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://learnperl.org/?p=49</guid>
		<description><![CDATA[Regular expressions describe text patterns. Each text pattern in a regular expression is called a metacharacter. =~ is the operator used for regular expressions. When characters are written between [ and ] it means they are part of a character &#8230; <a href="/lesson-04-regular-expressions/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Regular expressions describe text patterns.</p>
<p>Each text pattern in a regular expression is called a metacharacter.</p>
<p>=~ is the operator used for regular expressions.</p>
<p>When characters are written between [ and ] it means they are part of a character class. One character from the character class must match in order  to continue evaluating the rest of the regular expression.</p>
<p>Inside a character class, &#8211; indicates a range and ^ indicates negation.</p>
<p>Perl has shortcuts for the most common character classes.</p>
<p><tt>[a-zA-Z0-9_] can be written as \w and <tt>[^a-zA-Z0-9_] as \W.</tt><br />
</tt></p>
<p>Metacharacters.</p>
<ul>
<li>. means match any character except a newline</li>
<li>\w means match any alphanumeric character or the underscore</li>
<li>\W means match any character that is not alphanumeric or the underscore</li>
<li>\d means match any character that is a digit</li>
<li>\D means match any character that is not a digit</li>
<li>\s means match any character that is a whitespace such as a space, newline or a tab</li>
<li>\S means match any character that is not a whitespace</li>
<li>^ means match the beginning of the line</li>
<li>$ means match the end of the line</li>
</ul>
<p>^ and $ are called anchor metacharacters. They&#8217;re also sometimes called assertions.</p>
<p>Quantifiers describe how many times a character can be found in a string.</p>
<ul>
<li>* means zero or more</li>
<li>+ means one or more</li>
<li>? means zero or one time</li>
<li>{n} means n times where n is an integer</li>
<li>{n,m}means any number of times between n and m</li>
<li>{n,} means n or more times</li>
</ul>
<p>Modifiers.</p>
<ul>
<li>i (Ignore case)</li>
<li>s (Single line)</li>
<li>u (Unicode)</li>
<li>m (Multiline)</li>
<li>x (Verbose)</li>
<li>l (Locale)</li>
</ul>
<p>m/<em>regular expression here</em>/ is the same as /<em>regular expression here</em>/. It checks whether the first operand matches the text pattern.</p>
<p>s/<em>find this regular expression</em>/<em>replace with this text</em>/</p>
<p>Regex can be used to find a certain text and substitute it with another text.</p>
<p>The following example substitutes spaghetti with pizza:</p>
<pre>#!/usr/bin/perl

use strict;
use warnings;

my $sentence = "I love eating spaghetti.";

$sentence =~ s/spaghetti/pizza/;

print $sentence, "\n";</pre>
<p>This example substitutes the number of slices to 4:</p>
<pre>my $order = "3 slices of plain pizza
5 slices of pepperoni pizza";

$order =~ s/\d+/4/g;
print "Your order has been changed to:\n", $order, "\n";</pre>
<p>/g modifier means match the regex globally so it replaces all occurrences of a digit to 4.</p>
<p>The program prints this on the screen:</p>
<pre>Your order has been changed to:
4 slices of plain pizza
4 slices of pepperoni pizza</pre>
<p>When you want to take a portion of a string based on your regular expression, you must put parentheses around each pattern that you want to match. First matching part will be stored in $1, second matching part will be stored in $2, etc. We call this process capturing.</p>
<p>If you read <a href="http://perldoc.perl.org/perlrequick.html">perlrequick</a>, there is this example:</p>
<pre>($hours, $minutes, $second) = ($time =~ /(\d\d):(\d\d):(\d\d)/);</pre>
<p>It&#8217;s capturing this:</p>
<pre>($time =~ /(\d\d):(\d\d):(\d\d)/) # returns $1, $2, $3</pre>
<p>The values are assigned to ($hours, $minutes, $second)<br />
You need the parentheses to group the expression like this. Otherwise it&#8217;d first assign $time to $hours, then check $second (undef) against the regex. (Precedence issue with = and =~)</p>
<p>Notes: In Programming Perl, it says that an easy mistake is to think that <tt>\w</tt> matches a word. Use <tt>\w+</tt> to match a word.</p>
<p>When you&#8217;re learning how to make regex, I found this very useful. <a href="http://gskinner.com/RegExr/">http://gskinner.com/RegExr/</a></p>
]]></content:encoded>
			<wfw:commentRss>https://learnperl.org/lesson-04-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lesson 03 &#8211; Loop</title>
		<link>https://learnperl.org/lesson-03-loop/</link>
		<comments>https://learnperl.org/lesson-03-loop/#comments</comments>
		<pubDate>Sat, 29 Sep 2012 02:34:32 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://learnperl.org/?p=30</guid>
		<description><![CDATA[Looping is useful when you want to execute a block of code several times. In Perl, there are four styles of loops. #!/usr/bin/perl use strict; use warnings; my @pizza = ("plain", "pepperoni", "supreme", "veggie"); foreach (@pizza) { print $_, "\n"; &#8230; <a href="/lesson-03-loop/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Looping is useful when you want to execute a block of code several times.</p>
<p>In Perl, there are four styles of loops.</p>
<pre>#!/usr/bin/perl

use strict;
use warnings;

my @pizza = ("plain", "pepperoni", "supreme", "veggie");

foreach (@pizza)
{
    print $_, "\n";
}</pre>
<p>When working with arrays, foreach is usually preferred. In the above code, it loops until you reach the end of the array.</p>
<p>In Perl, foreach can be written for.</p>
<pre>for (@pizza)
{
    print $_, "\n";
}</pre>
<p>$_ is a special kind of variable (the topic variable) that will get an element from the array on each iteration. In the Modern Perl book, the author describes the usage of $_ as similar to the word &#8220;it&#8221; in the English language.</p>
<pre>for (my $i=0; $i&lt;@pizza; $i++)
{
    print $pizza[$i], "\n";
}</pre>
<p>The C-style for loop does the same thing but it requires a conditional so that it continues to loop and increment $i by 1 until $i is greater than or equal to the size of the array.</p>
<p>The Perl-style for loop iterating with index over an array is this:</p>
<pre>
for my $m (0..$#pizza) 
{
     print $pizza[$m], "\n";
}
</pre>
<p>.. is Perl&#8217;s range operator and $#pizza is the highest index.</p>
<pre>my $k = 0;

while ($k&lt;@pizza)
{
    print $pizza[$k], "\n";
    $k++;
}</pre>
<p>This is the while loop that does exactly the same thing. It loops as long as $k is less than the size of the array.</p>
<p>It will exit the loop when the condition is false.</p>
<p>The until loop is the opposite of the while loop. It will loop and exit when the condition is true.</p>
<pre>my $j = 0;

until ($j&gt;=@pizza)
{
    print $pizza[$j], "\n";
    $j++;
}</pre>
<p>Notice how @pizza is used instead of scalar(@pizza). That&#8217;s because scalar context is implicit there.</p>
]]></content:encoded>
			<wfw:commentRss>https://learnperl.org/lesson-03-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lesson 02 &#8211; Conditional</title>
		<link>https://learnperl.org/lesson-02-conditional/</link>
		<comments>https://learnperl.org/lesson-02-conditional/#comments</comments>
		<pubDate>Fri, 28 Sep 2012 21:31:52 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://learnperl.org/?p=18</guid>
		<description><![CDATA[If you want to do something based on a certain criteria, you should use a conditional. #!/usr/bin/perl use strict; use warnings; my $pizza = "pepperoni"; if ($pizza eq "pepperoni") { print "Yes, my pizza is pepperoni!\n"; } elsif ($pizza eq &#8230; <a href="/lesson-02-conditional/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you want to do something based on a certain criteria, you should use a conditional.</p>
<pre>#!/usr/bin/perl

use strict;
use warnings;

my $pizza = "pepperoni";

if ($pizza eq "pepperoni")
{
     print "Yes, my pizza is pepperoni!\n";
}
elsif ($pizza eq "plain")
{
     print "Plain pizza is good too.\n";
}
else
{
     print "I don't want it.\n";
}</pre>
<p>The eq operator checks if the two strings match.</p>
<p>If you are dealing with numbers, you should be using one of these numeric comparison operators:</p>
<ul>
<li>== checks whether two numbers are equal</li>
<li>&gt; checks whether the first number is greater than the second number</li>
<li>&gt;= checks whether the first number is greater than or equal to the second number</li>
<li>&lt; checks whether the first number is less than the second number</li>
<li>&lt;= checks whether the first number is less than or equal to the second number</li>
<li>!= checks whether the first number is not equal to the second number</li>
<li>&lt;=&gt; is the sort comparison operator 1 is returned if the first number is greater than the second number. 0 is returned if the first number is equal to the second number. -1 is returned if the first number is less than the second number.</li>
</ul>
<p>When using simple expressions, you can use the postfix form (also called the statement modifier) which looks something like this:</p>
<pre>print "I like pepperoni pizza." if $pizza eq "pepperoni";</pre>
<p>Notice how it doesn&#8217;t require any curly braces for the postfix form.</p>
<pre>unless (defined($pizza))
{
    print "Tell me what kind of pizza you want.\n";
}</pre>
<p>The conditional means the same thing as &#8220;if $pizza is not defined&#8221;.</p>
<pre>if (!defined($pizza))
{
    print "Tell me what kind of pizza you want.\n";
}</pre>
<p>The ternary conditional operator is the same as using if and else. It looks like this:</p>
<pre>$pizza eq "pepperoni" ? print "I like pepperoni pizza." : print "I don\'t want anything else.";</pre>
<p>given/when is equivalent to an if/elsif chain</p>
<pre>
given ($pizza)
{
when "pepperoni" { print "Yes, my pizza is pepperoni!\n"; }
when "plain" { print "Plain pizza is good too.\n"; }
default { print "I don't want it.\n"; }
}
</pre>
<p>If you&#8217;re familiar with other programming languages like C, given/when is basically switch/case spelled differently.</p>
<p>Notes: expression of &#8216;given&#8217; is in scalar context, and always assigns to $_ (topic variable). </p>
<p>$_ is available in that block of code. It has lexical scope.</p>
<p>&#8216;when&#8217; is actually not required in a &#8216;given&#8217; block. It&#8217;s simply a way of quickly testing a value by assigning it to $_</p>
<p>given/when does an implicit smart match</p>
<pre>
given (whatever()) { when (10) { ... } }
</pre>
<p>when (10) is implicitly when ($_ ~~ 10)</p>
]]></content:encoded>
			<wfw:commentRss>https://learnperl.org/lesson-02-conditional/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lesson 01 &#8211; Variables</title>
		<link>https://learnperl.org/lesson-01-variables/</link>
		<comments>https://learnperl.org/lesson-01-variables/#comments</comments>
		<pubDate>Fri, 28 Sep 2012 17:03:54 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://learnperl.org/?p=6</guid>
		<description><![CDATA[A variable is used by a program to store a value. In Perl, there are three different types of variables. Scalar &#8211; one value of either a number, a string, a dualvar, a reference or undef Array &#8211; a set &#8230; <a href="/lesson-01-variables/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A variable is used by a program to store a value. In Perl, there are three different types of variables.</p>
<ul>
<li>Scalar &#8211; one value of either a number, a string, a dualvar, a reference or undef</li>
<li>Array &#8211; a set of values of indexed values starting with position 0</li>
<li>Hash &#8211; a set of key-value pairs</li>
</ul>
<p>Scalars use $ in front of the variable name. Array use @ in front of the variable name. Hashes use % in front of the variable name.</p>
<pre>#!/usr/bin/perl

use strict;
use warnings;

my $pizza = "plain";
my @pizza = ("plain", "pepperoni", "supreme", "veggie");
my %pizza = ("plain" =&gt; 5, "pepperoni" =&gt; 7, "supreme" =&gt; 6, "veggie" =&gt; 4);

print "The pizza I ordered is ", $pizza, "\n";
print "The second pizza on the menu is ", $pizza[1], "\n";
print "The restaurant has ", scalar(@pizza), " kinds of pizza\n";
print "The restaurant has ", scalar(keys(%pizza)), " kinds of pizza\n";
print "The restaurant has ", $pizza{"veggie"}, " of the veggie pizzas\n";</pre>
<p>When you run this program, it will print this on the screen:</p>
<pre>The pizza I ordered is plain
The second pizza on the menu is pepperoni
The restaurant has 4 kinds of pizza
The restaurant has 4 kinds of pizza
The restaurant has 4 of the veggie pizzas</pre>
<p>Like in many other programming languages, arrays start at position 0 for the first element, position 1 for the second element, position 2 for the the third element, etc.</p>
<p>The function scalar() counts the total number of elements of a list.</p>
<p>The function keys() grabs the keys of a hash and puts it in a list.</p>
<p>When you want to grab a value from a hash, you put the key value in between curly braces.</p>
<p>It&#8217;s important to understand the context of which you are using variables. Is the variable in scalar context or list context?</p>
]]></content:encoded>
			<wfw:commentRss>https://learnperl.org/lesson-01-variables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
