[Purdue-pm] using Perl 6

Mark Senn mark at purdue.edu
Thu Jun 3 04:47:09 PDT 2010


I istalled the latest Rakudo (Perl 6) on my home
computer (Fedora 13 Linux) last night using the
instructions in http://www.rakudo.org/how-to-get-rakudo:
    $ git clone git://github.com/rakudo/rakudo.git
    $ cd rakudo
    $ perl Configure.pl --gen-parrot
    $ make
(forget if I did next command or not)
    $ make install

Haven't found anything that is broken yet...just tried some
simple stuff though.

Thought you might be interested in following message.  I
found the discussion following the
    === while loop ===
heading interesting.

-mark

>Date: Thu, 3 Jun 2010 11:51:36 +0300
>From: Gabor Szabo <szabgab at gmail.com>
>To: perl6 at szabgab.com
>Subject: [Perl6 Tricks and Treats] Staring with Perl 6
>
>==== Welcome back to the Perl 6 Tricks and Treats ====
>
>My 12 year old son had several attempts to learn programming
>already. He started to build a web application in Perl but
>without the basics and without understand English he kept
>getting stuck with simple things such as references and
>5 dimensional hashes.
>
>A few days ago we decied to put him through my regular Perl 5
>training material that will let him learn the basics. He seems
>to be mature enough now that he can find the joy in creating
>even console based games. At least he might understand the
>need to go a bit slower and not jump into a web application.
>
>I started to teach him Perl 5 and programming in general.
>We decided on Perl 5 as that's what I know the most and
>its a mature language so he will be able to continue to write
>his web application.
>In a few days we learned about scalars, basic I/O (print and <STDIN>),
>chomp, if conditions, strings and numbers, while loops, int, rand.
>We already started to learn about reading and writing files.
>
>All this time I kept feeling that my training material does not fit
>someone who does not have the basic concepts of a computer yet and
>has no background in programming. Also there were all kinds of things
>I had to explain that were just not clear. e.g. the need for chomp,
>why to write the "or die" part of open() at all?
>
>The former I could manage by adjusting the lectures to his needs,
>skipping slides that were not relevant and adding more explanations
>where needed. I could not deal with the latter part, that are just
>issues with Perl 5.
>
>Yesterday I decided that I stop the whole Perl 5 course and switch
>over to teach him Perl 6. After all his main interest is to learn
>programming and to build a game for himself and for his frinds.
>It does not need to be production grade and by the time he reaches
>the point that he wants this to be on a public server we will have
>a much more mature Perl 6 implementation.
>
>This will also give me an opportunity to go over my training
>material with the eyes of someone totally new to programming.
>
>==== Upcoming Courses ====
>
>This year I am not running Perl 6 classes or any classes next
>to the YAPCs but Damian Conway does:
>
>== Hands-on Perl 6 with Rakudo ==
>
>   June 25  Registration via the YAPC::NA web site
>
>   http://yapc2010.com/yn2010/talk/2765
>
>==== Getting Rakudo ====
>
>In order to make life easier for my son I built Rakudo
>for him follow the instructions on
>
>http://www.rakudo.org/how-to-get-rakudo
>
>I added the directory of rakudo to the PATH so it will be easy
>for him to run perl6 from the comman line.
>
>BTW he is using Ubuntu 10.4
>
>
>==== Setting up Padre for Perl 6 ====
>
>I also installed Padre from CPAN (using local::lib)
>and installed the Padre::Plugin::Perl6 from CPAN.
>
>Once I launched Padre I went to Tools/Plug-in Manager and
>enabled the Perl 6 plugin.
>
>Then I went to Tools/Preferences/File and colors
>in File type: I selected Perl 6 and in Highlighter STD.pm
>That will provide syntax highlighting for files with p6 extension.
>
>I also turned on View/Show Syntax check
>
>==== Perl 6  ====
>
>Let's start the code now:
>
> use v6;              # Even though this is not enforced when using
>                      # Rakudo better to declare the version of perl.
>                      # This will avoid lots of head scratching
>                      # when you run it with perl 5 by mistake.
>
> say 42;              # prints 42 and a newline
> say "hello world";   # prints hello world and a newline
>
>
>This was the first time, after about 2 minues in Perl 6 that he said
>"Oh that's simpler".
>
>Values need to be stored in boxes .... so the values of the boxes
>can be changed later. Therefore the boxes are called "variables".
>The scalar values such as numbers and strings can be stored in so called
>scalar variables.
>
> my $x = 42;          # Scalar variables start with $ and include
>                      # letters and numbers and underscore.
>                      # They are declared using the "my" keyword.
> say $x;              # They can be used just as regular scalar values.
>
>Earlier I already explained to him that this is different from math.
>Here the = sign means assignment. So the previous statement means
>put 42 in the box that is called $x.
>
> my $name = prompt("type your name: ");
>
>                      # This will print the string on the screen and then
>                      # wait for the user to type in some text and press enter.
>                      # This replaces the <STDIN> we had in perl 5.
>
>He asked where is chomp. There is no need for chomp here as prompt()
>automatically removes the newlines.
>"Oh nice", he said.
>
>
>=== Conditionals ===
>
> if $age == 12 {
>     say "he is 12";
> }
>
>The if conditional does not need to have parentheses around it.
>This looks strange to me but it he found it simpler.
>
>I had to point out here that we have ==, two equeal signs that mean
>we are not assigning values but checking if they are equal or not.
>I also had to explain about the difference between numerical ==
>and string 'eq'. I think this will need more explanation though.
>
>
>else, and elseif are the same as in Perl 5 so you can write:
>
> if $age < 12 {
>     # young
> } else {
>     # old
> }
>
>or you can write
>
> if $age < 11 {
>     # too young
> } elsif $age > 13 {
>     # to old
> } else {
>     # good
> }
>
>
>Then I showed him the chained comparison possibilities:
>
> if (11 <= $age <= 13) {
>     say "Good, $age is in the range. We can play soccer.";
> } else {
>     say "No partner for game";
> }
>
>He loved that it is just like in math.
>
>
>=== while loop ===
>
>For English speakers it might be obvious what a while loop does
>but others better translate the word for themselves and build a
>real sentence with it.
>In English it sound like this:
>
>While this is true do this.
>
>In perl 6 it looks like this:
>
> while this is true {
>     do this
> }
>
>and with a real example it looks like this:
>
> my $num = 50;
> while $num >= 50 {
>     $num = prompt("Please give a number smaller than 50: ");
> }
>
>Actually my son kept writing code like this:
>
> my $num = prompt("Please give a number smaller than 50: ");
> while $num >= 50 {
>     $num = prompt("Please give a number smaller than 50: ");
> }
>
>I am not 100% sure how to elminate that duplication in a way that
>will make sense to him as well. He thinks about this as
>"Ask the user. If the answer is incorrect, ask again."
>
>I would probably write this code:
>
> my $num;
> while not defined $num or $num >= 50 {
>     $num = prompt("Please give a number smaller than 50: ");
> }
>
>which translates to "While no correct value ask for a value".
>
>Maybe this would make more sense though:
>
> my $num = prompt("Please give a number smaller than 50: ")
>     while not defined $num or $num >= 50;
>
>Which is "Ask for a value as long as it is incorrect".
>
>
>The "not defined $num" part still seems to be as unnecessary in
>this case but that is currently required.
>
>
>I asked on the #perl6 IRC channel for a more natural solution.
>I got several suggestions in a few seconds. The one that seemed
>to make sense the most to me was:
>
> while (my $num = prompt("Please give a number smaller than 50: ")) >= 50 {}
>
>Here we need the extra parentheses around the assignment or the >= comparision
>would take precedence.
>
>
>
>There was one thing that tripped us is that code like this:
>
> while ($num >= 50){
> }
>
>is incorrect. People who are used to write extensive spaces won't
>fall in that trap but many beginners skip spaces in all kinds
>of places.
>
>In this case you need to either add a space between the
>closing parentheses and the  opening curly or omit the parentheses at all.
>
>That's all for now.
>
>
>=== Comments and Discussion ===
>
>I am always open to comments and criticism
>(just have a positive spin to it :-)
>So if you find any issue with the examples,
>please don't hesitate to let me know.
>
>If you'd like to ask question about Perl 6,
>probably the best would be to sign up on the Perl 6
>users list by sending an e-mail to
>
>   perl6-users-subscribe at perl.org
>
>The archive of the perl6-users list is at:   http://www.perl6.org/
>
>The even better way is to join the #perl6 IRC channel on irc.freenode.net
>If you have not used IRC yet, the easies way is to use the web based
>IRC client of Freenode http://webchat.freenode.net/?channels=perl6
>
>Previous issues of this newsletter can be found on
>
>   http://szabgab.com/perl6_tricks_and_treats.html
>
>
>==== Copyright ====
>
>Perl 6 Tricks and Treats and associated text is
>Copyright 2009-2010 Gabor Szabo <gabor at szabgab.com>
>The specific posts are Copyright the respective authors.
>You may freely distribute this text so long as it is distributed
>in full with this Copyright noticed attached.
>
>If you have any questions please don't hesitate to contact me:
>Email: szabgab at gmail.com
>Web:   http://szabgab.com/
>_______________________________________________
>Perl6 mailing list
>Perl6 at szabgab.com
>http://mail.szabgab.com/mailman/listinfo/perl6


More information about the Purdue-pm mailing list