<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>offer.kaye</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/offerkaye/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.perl.org/users/offerkaye/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/offerkaye//350</id>
    <updated>2010-08-10T11:56:59Z</updated>
    <subtitle>A blog about the Perl programming language</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>P6 Gentle Intro, Part 2</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/offerkaye/2010/07/p6-gentle-intro-part-2.html" />
    <id>tag:blogs.perl.org,2010:/users/offerkaye//350.812</id>

    <published>2010-07-31T18:56:07Z</published>
    <updated>2010-08-10T11:56:59Z</updated>

    <summary>Part 1 available here. Arrays A Perl 6 arrays is an ordered container for a list of scalar items - meaning, you can count on the order of items in the array. Here are some ways to create lists and...</summary>
    <author>
        <name>Offer Kaye</name>
        
    </author>
    
    <category term="arrays" label="arrays" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="perl6" label="perl6" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="rakudo" label="rakudo" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/offerkaye/">
        <![CDATA[<p><small><a href="http://blogs.perl.org/users/offerkaye/2010/07/a-gentle-introduction-to-perl-6-using-rakudo-star.html">Part 1 available here</a>.</small></p>

<h3>Arrays</h3>

<p>A Perl 6 arrays is an ordered container for a list of scalar items - meaning, you can count on the order of items in the array. Here are some ways to create lists and assign them to an array:</p>

<pre><code>my @primes1 = 2 , 3 , 5 , 7 , 11 , 13; # using the comma operator
my @primes2 = &lt;17 19 23 29 31>;        # same as the comma seperated list
my @primes3 = @primes1 , @primes2;     # this works, giving a single combined list
</code></pre>

<p>Functions and operators which return lists also work. For example the range operator:</p>
<pre><code>my @range1 = 1 .. 10;
my @range2 = 'a' .. 'j' ; # it's magic :)
</code></pre>

<p>Note that assigning a list to an array will "use up" all available items - the following will not assign anything to @p2:</p>
<pre><code>my (@p1 , @p2) =  &lt;2 3 5 7 11 13> , &lt;17 19 23 29 31>; # @p1 is the same as @primes3, @p2 is an empty array
</code></pre>

<p>In order to get a specific value from an array we add square brackets after the array name and use an integer index starting from 0 (for the first element). Here are some examples:</p>
<pre><code>my @primes = &lt;2 3 5 7 11 13>;

my $first  = @primes[0];  # $p1 = 2
my $last   = @primes[@primes.end]; # @primes.end - the last index
</code></pre>

<p>Although all of my examples so far have been of arrays containing only numbers, lists and arrays can as I said contain any scalar. For example:</p>
<pre><code>my @mixed = "this list" , 'has' , 4 , "elements";
</code></pre>

<p>To print all the elements of the list or array you could do something like "@array.say" or "say @array" (which is the same) but the output would be all of the elements without any spaces between them - not very pretty:</p>
<pre><code>@mixed.say ; # prints "this listhas4elements"
</code></pre>
<p>Perl 6 provides us instead with several different options:</p>
<pre><code>@mixed.perl.say ; # prints the data structure as perl6 understands it. In this case: ["this list", "has", 4, "elements"]  -- great for debugging!

say "@mixed[]"; # prints "this list has 4 elements" -- Adding empty square brackets to the array var inside double quotes causes a whitespace seperated list of values to be printed.
say "{@mixed}"; # exactly the same output, but relies on curly braces doing interpolation
</code></pre>

<p>Finally here is a random selection of a few array/list methods:
<pre><code>my @r1 = 1 .. 10;

@r1.pop.say; # remove the last element from an array and return it
@r1.perl.say ; # now only 1 .. 9

@r1.push(11); # append one or more elements to an array. In this example, 11
@r1.perl.say ; # now 1 .. 9 , 11

@r1.reverse.perl.say ; # 11 , 9 .. 1
</code></pre>

<p>See <a href="http://perlcabal.org/syn/S32/Containers.html#List">Synopsys 32 - Containers</a> for more list and array methods.</p>]]>
        
    </content>
</entry>

<entry>
    <title>A Gentle Introduction to Perl 6 Using Rakudo Star</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/offerkaye/2010/07/a-gentle-introduction-to-perl-6-using-rakudo-star.html" />
    <id>tag:blogs.perl.org,2010:/users/offerkaye//350.809</id>

    <published>2010-07-30T20:41:32Z</published>
    <updated>2010-07-30T21:22:02Z</updated>

    <summary>So, Rakudo Star was recently released and I decided to give it a try. So far, I like what I&apos;m seeing. The below is meant to serve as a gentle introduction to the Perl 6 language, if it seems to...</summary>
    <author>
        <name>Offer Kaye</name>
        
    </author>
    
    <category term="perl6" label="perl6" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="rakudo" label="rakudo" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/offerkaye/">
        <![CDATA[So, Rakudo Star was recently released and I decided to give it a try. So far, I like what I'm seeing.<br />
<p>The below is meant to serve as a gentle introduction to the Perl 6 language, if it seems to be very "baby-Perl-6" that's because I'm learning the language as I'm writing it. Plus, that's why I called it a <strong>gentle</strong> introduction! :)</p>

<p><u><strong><big>Installation on Windows 7</big></strong></u></p>
<p>Download the <a href="http://github.com/downloads/rakudo/star/rakudo-star.2010.07.msi">msi file available from GitHub</a>, double-click and answer the questions with "yes".<br />
The Perl 6 executables will be installed under C:\Rakudo\bin.<br />
Open the Control Panel and go to "System and Security" -> "System". In the left side of the window you should see 4 links, click the bottom one - "Advanced system settings". In the window that opens ("System Properties" - "Advanced" tab), click on the "Environment Variables..." button, which is at the bottom. In the bottom part of the window which opens, there is a "System variables" window. Scroll down to the "Path" variable, make sure it is selected and hit the "Edit..." button. Jump to the end of the "Variable value" field and add ";C:\Rakudo\bin" to the end - the ";" is important. Hit "OK", "OK", "OK". Now open a new cmd.exe window and type "perl6 -v". You should get the output:</p>

<blockquote>This is Rakudo Perl 6, version 2010.07-47-g9fd5eaa built on parrot 2.6.0

Copyright 2008-2010, The Perl Foundation
</blockquote>

<p>Congratulations, you're ready to program in Perl 6 on Windows 7 :)</p>

<p><u><strong><big>Hello, world!</big></strong></u></p>
Open your text editor, type:<br />

<code>say 'Hello, world!';</code>

<p>Save the file as hello.p6. In your cmd.exe window cd to the directory where you saved hello.p6 and type "perl6 hello.p6". You should now see the output:</p>
<blockquote>Hello, world!</blockquote>
<p>Already in this simple program we see a few elements of Perl 6:</p>
<ol>
	<li>"say" is a function which prints to the standard output (in this case) the given argument.</li>
	<li>'Hello, world!' is a string literal. A string is a piece of text, and a string literal is a string which appears directly in the program. There are 2 types of string literals in Perl 6, double-quoted (e.g. "hello") and single quoted (e.g. 'hello'). Since we're not using any variables in this example, there is no need to interpolate, so we use single-quotes.</li>
	<li>A Perl 6 program consists of zero or more statements. A statement ends with a semicolon or a curly bracket at the end of a line. In this case, a semicolon.</li>
</ol>

<p><u><strong><big>use v6;</big></strong></u></p>
<p>Open hello.p6 in your editor and type as the first line:</p>
<blockquote>use v6;</blockquote>
<p>Your program should now look like:</p>
<code>
use v6;
say 'Hello, world!';
</code>

<p>While not strictly required, every Perl 6 program should begin with "use v6;". This line tells the compiler which version of Perl the program expects. Should you accidentally run the file with Perl 5, you'll get a helpful error message.</p>

<p><u><strong><big>Semicolons and Newlines</big></strong></u></p>
<p>Now that we have 2 statements we can see that in Perl 6 a statement really does end with a semicolon. Change the program to a single line, like so:</p>

<blockquote>use v6; say 'Hello, world!';</blockquote>

<p>If you run this program it will run the same as before. Perl 6 does not care whether there is a newline or not between statements. Vice-versa, if you forget the semicolon and only use a newline, you'll get an error:</p>

<code>
use v6
say 'Hello, world!';
</code>

<p>Output:</p>

<pre>
===SORRY!===
Confused at line 1, near "use v6\r\nsa"
</pre>

<p>The "\r\n" is the way Windows refers to a newline. I'll usually call it simply "\n" which is the Unix way.</p>

<p>As the last statement in the file, you could if you wanted remove the ';' at the end of the say line. However it is good practise to keep the ';' for the last statement, in case in the future you should modify the file by adding more statements.</p>

<p><u><strong><big>Comments</big></strong></u></p>
<p>Comments start with a # sign and last till the end of the line. A "#" sign inside a string however does not start a comment.<br />

<a href="http://perlcabal.org/syn/S02.html">Synopys 02</a> has a lot more to say about whitespaces and comments.</p>

<p><u><strong><big>Strings</big></strong></u></p>
<p>Apart from simply printing a string's contents there are a variety of other interesting things we can do with strings in Perl 6. For example:</p>

<code>
'Hello, world!'.say;             # same as "say 'Hello, world!'"
'Hello, world!'.chars.say;       # prints "13" which is the length in characters of the string. This also shows that chaining of methods works.
'Hello, world!'.substr(7).say;   # prints "world!". Offsets starts at 0.
'Hello, world!'.substr(0,4).say; # prints the first 4 characters
'Hello, world!'.uc.say;          # prints "HELLO, WORLD!"
'Hello, world!'.lc.say;          # prints "hello, world!"
'Hello, world!'.capitalize.say;  # prints "Hello, World!", i.e. it returns a lower case copy of the string with each first character in a word as upper case.
'Hello, world!'.chop.say;        # prints "Hello, world". You get extra points for using ".chop.chop" in a program :)
'Hello, world!'.flip.say;        # prints "!dlrow ,olleH"
</code>

<p>You can see these methods and more documented at the <a href="http://perlcabal.org/syn/S32/Str.html">S32 - Str Synopsys page</a>.</p>

<p>Strings in Perl 6 can be concatenated using the tilda (~) operator.</p>

<code>say 'Hello,' ~ ' world!'; # prints "Hello, world!"</code>

<p>If you're dealing with Unicode strings, make sure read the Perl 6 S32 document mentioned above, since you will need to be aware of certain finer points of how Unicode text is handeled in Perl 6.</p>

<p><u><strong><big>Scalars</big></strong></u></p>
<p>Variables in Perl 6 have names which start with a sigil, which is non-alpha-numeric symbol such as $, @, %, or & - or occasionally the double colon ::. The sigils usually restrict the variable to a particular type, such as a single value or a compound value. After the sigil comes an identifier, which may consist of letters, digits and the underscore. Between letters you can also use a dash - or an apostrophe ', so isn't and double-click are valid identifiers. First of all I'd like to talk about scalars.</p>

<code>
my $x;
$x = 42;             # Decimal Integer
$x = 0xF6;           # Hexadecimal Integer
$x = 0b1010010001;   # Binary Integer
 
$x = 3.1415;         # Floating Point Number
$x = 2.34E-5;        # Scientific Notation
 
$x = "Hello ";       # Double-Quoted String
$x = 'World!';       # Single-Quoted String

$x = MyObject.new(); # Object Reference
</code>

<p>The "my" is another Perl 6 function, this one declares a variable local to the enclosing scope (or till the end of the file, if decalred in the file scope). For those coming from a Perl 5 background, using a variable before declaring it (with "my" for example) will no longer work. You must, in other words, declare a variable, for example with "my", before trying to use it.<br /><br />

Note that declaring "$x = 42" does not prevent Perl 6 from treating 42 as a string, and vice-versa:</p>
<code>
my $x = 42;
"The answer to life, the universe and everything is... $x!".say ;# works as expected

my $sum = 1 + "5";
$sum.say; ;# prints 6, the string "5" is used as a number.
</code>

<p>See <a href="http://perlcabal.org/syn/S02.html">S02</a> for a few more advanced ways to define a scalar variable, e.g. "my Int $x;". Not going to talk about that here, this is a gentle introduction :)</p>


<p>More to come at later time: arrays, hashes, flow control and of course, regular expressions. That's all for now :)</p>
]]>
        
    </content>
</entry>

</feed>
