<?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/" xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/" ><channel>
    <title>Perlgeek.de   </title>
    <link>http://perlgeek.de/blog-en/</link>
    <description>Perl and Programming Blog.</description>

  <item>
 <title>Currying</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/28-currying.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Sun, 25 Jul 2010 09:17:10 +0100</pubDate>
    <description>&lt;!-- 1280049430 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 28 - Currying&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;  use v6;
  
  my &amp;#38;f := &amp;#38;substr.assuming(&amp;#39;Hello, World&amp;#39;);
  say f(0, 2);                # He
  say f(3, 2);                # lo
  say f(7);                   # World
  
  say &amp;#60;a b c&amp;#62;.map: * x 2;     # aabbcc
  say &amp;#60;a b c&amp;#62;.map: *.uc;      # ABC
  for ^10 {
      print &amp;#60;R G B&amp;#62;.[$_ % *]; # RGBRGBRGBR
  }&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;i&gt;Currying&lt;/i&gt; or &lt;i&gt;partial application&lt;/i&gt; is the process of generating a function from another function or method by providing only some of the arguments. This is useful for saving typing, and when you want to pass a callback to another function.&lt;/p&gt;

&lt;p&gt;Suppose you want a function that lets you extract substrings from &lt;code&gt;&amp;#34;Hello, World&amp;#34;&lt;/code&gt; easily. The classical way of doing that is writing your own function:&lt;/p&gt;

&lt;pre&gt;  sub f(*@a) {
      substr(&amp;#39;Hello, World&amp;#39;, |@a)
  }&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Currying_with_assuming&quot;
&gt;Currying with &lt;code&gt;assuming&lt;/code&gt;&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Perl 6 provides a method &lt;code&gt;assuming&lt;/code&gt; on code objects, which applies the arguments passed to it to the invocant, and returns the partially applied function.&lt;/p&gt;

&lt;pre&gt;  my &amp;#38;f := &amp;#38;substr.assuming(&amp;#39;Hello, World&amp;#39;);&lt;/pre&gt;

&lt;p&gt;Now &lt;code&gt;f(1, 2)&lt;/code&gt; is the same as &lt;code&gt;substr(&amp;#39;Hello, World&amp;#39;, 1, 2)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;assuming&lt;/code&gt; also works on operators, because operators are just subroutines with weird names. To get a subroutine that adds 2 to whatever number gets passed to it, you could write&lt;/p&gt;

&lt;pre&gt;  my &amp;#38;add_two := &amp;#38;infix:&amp;#60;+&amp;#62;.assuming(2);&lt;/pre&gt;

&lt;p&gt;But that&amp;#39;s tedious to write, so there&amp;#39;s another option.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Currying_with_the_Whatever-Star&quot;
&gt;Currying with the Whatever-Star&lt;/a&gt;&lt;/h4&gt;

&lt;pre&gt;  my &amp;#38;add_two := * + 2;
  say add_two(4);         # 6&lt;/pre&gt;

&lt;p&gt;The asterisk, called &lt;i&gt;Whatever&lt;/i&gt;, is a placeholder for an argument, so the whole expression returns a closure. Multiple Whatevers are allowed in a single expression, and create a closure that expects more arguments, by replacing each term &lt;code&gt;*&lt;/code&gt; by a formal parameter. So &lt;code&gt;* * 5 + *&lt;/code&gt; is equivalent to &lt;code&gt;-&amp;#62; $a, $b { $a * 5 + $b }&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;  my $c = * * 5 + *;
  say $c(10, 2);                # 52&lt;/pre&gt;

&lt;p&gt;Note that the second &lt;code&gt;*&lt;/code&gt; is an infix operator, not a term, so it is not subject to Whatever-currying.&lt;/p&gt;

&lt;p&gt;The process of lifting an expression with Whatever stars into a closure is driven by syntax, and done at compile time. This means that&lt;/p&gt;

&lt;pre&gt;  my $star = *;
  my $code = $star + 2&lt;/pre&gt;

&lt;p&gt;does not construct a closure, but instead dies with a message like&lt;/p&gt;

&lt;pre&gt;  Can&amp;#39;t take numeric value for object of type Whatever&lt;/pre&gt;

&lt;p&gt;Whatever currying is more versatile than &lt;code&gt;.assuming&lt;/code&gt;, because it allows to curry something else than the first argument very easily:&lt;/p&gt;

&lt;pre&gt;  say  ~(1, 3).map: &amp;#39;hi&amp;#39; x *    # hi hihihi&lt;/pre&gt;

&lt;p&gt;This curries the second argument of the string repetition operator infix &lt;code&gt;x&lt;/code&gt;, so it returns a closure that, when called with a numeric argument, produces the string &lt;code&gt;hi&lt;/code&gt; as often as that argument specifies.&lt;/p&gt;

&lt;p&gt;The invocant of a method call can also be Whatever star, so&lt;/p&gt;

&lt;pre&gt;  say &amp;#60;a b c&amp;#62;.map: *.uc;      # ABC&lt;/pre&gt;

&lt;p&gt;involves a closure that calls the &lt;code&gt;uc&lt;/code&gt; method on its argument.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Perl 5 could be used for functional programming, which has been demonstrated in Mark Jason Dominus&amp;#39; book &lt;i&gt;Higher Order Perl&lt;/i&gt;.&lt;/p&gt;

&lt;p&gt;Perl 6 strives to make it even easier, and thus provides tools to make typical constructs in functional programming easily available. Currying and easy construction of closures is a key to functional programming, and makes it very easy to write transformation for your data, for example together with &lt;code&gt;map&lt;/code&gt; or &lt;code&gt;grep&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://design.perl6.org/S02.html#Built-In_Data_Types&quot; class=&quot;podlinkurl&quot;
&gt;http://design.perl6.org/S02.html#Built-In_Data_Types&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://hop.perl.plover.com/&quot; class=&quot;podlinkurl&quot;
&gt;http://hop.perl.plover.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Currying&quot; class=&quot;podlinkurl&quot;
&gt;http://en.wikipedia.org/wiki/Currying&lt;/a&gt;&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>Common Perl 6 data processing idioms</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/27-common-idioms.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Thu, 22 Jul 2010 13:34:26 +0100</pubDate>
    <description>&lt;!-- 1279805666 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 27 - Common Perl 6 data processing idioms&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;  # create a hash from a list of keys and values:
  # solution 1: slices
  my %hash; %hash{@keys} = @values;
  # solution 2: meta operators
  my %hash = @keys Z=&amp;#62; @values;

  # create a hash from an array, with
  # true value for each array item:
  my %exists = @keys X=&amp;#62; True;

  # limit a value to a given range, here 0..10.
  my $x = -2;
  say 0 max $x min 10;

  # for debugging: dump the contents of a variable,
  # including its name, to STDERR
  note :$x.perl;

  # sort case-insensitively
  say @list.sort: *.lc;

  # mandatory attributes
  class Something {
      has $.required = die &amp;#34;Attribute &amp;#39;required&amp;#39; is mandatory&amp;#34;;
  }
  Something.new(required =&amp;#62; 2); # no error
  Something.new()               # BOOM&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Learning the specification of a language is not enough to be productive with it. Rather you need to know how to solve specific problems. Common usage patterns, called &lt;i&gt;idioms&lt;/i&gt;, helps you not having to re-invent the wheel every time you&amp;#39;re faced with a problem.&lt;/p&gt;

&lt;p&gt;So here a some common Perl 6 idioms, dealing with data structures.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Hashes&quot;
&gt;Hashes&lt;/a&gt;&lt;/h4&gt;

&lt;pre&gt;  # create a hash from a list of keys and values:
  # solution 1: slices
  my %hash; %hash{@keys} = @values;
  # solution 2: meta operators
  my %hash = @keys Z=&amp;#62; @values;&lt;/pre&gt;

&lt;p&gt;The first solution is the same you&amp;#39;d use in Perl 5: assignment to a slice. The second solution uses the zip operator &lt;code&gt;Z&lt;/code&gt;, which joins to list like a zip fastener: &lt;code&gt;1, 2, 3 Z 10, 20, 30&lt;/code&gt; is &lt;code&gt;1, 10, 2, 20, 3, 30&lt;/code&gt;. The &lt;code&gt;Z=&amp;#62;&lt;/code&gt; is a meta operator, which combines zip with &lt;code&gt;=&amp;#62;&lt;/code&gt; (the Pair construction operator). So &lt;code&gt;1, 2, 3 Z=&amp;#62; 10, 20, 30&lt;/code&gt; evaluates to &lt;code&gt;1 =&amp;#62; 10, 2 =&amp;#62; 20, 3 =&amp;#62; 30&lt;/code&gt;. Assignment to a hash variable turns that into a Hash.&lt;/p&gt;

&lt;p&gt;For existence checks, the values in a hash often doesn&amp;#39;t matter, as long as they all evaluate to &lt;code&gt;True&lt;/code&gt; in boolean context. In that case, a nice way to initialize the hash from a given array or list of keys is&lt;/p&gt;

&lt;pre&gt;  my %exists = @keys X=&amp;#62; True;&lt;/pre&gt;

&lt;p&gt;which uses the cross meta operator to use the single value &lt;code&gt;True&lt;/code&gt; for every item in &lt;code&gt;@keys&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Numbers&quot;
&gt;Numbers&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Sometimes you want to get a number from somewhere, but clip it into a predefined range (for example so that it can act as an array index).&lt;/p&gt;

&lt;p&gt;In Perl 5 you often end up with things like &lt;code&gt;$a = $b &amp;#62; $upper ? $upper : $b&lt;/code&gt;, and another conditional for the lower limit. With the &lt;code&gt;max&lt;/code&gt; and &lt;code&gt;min&lt;/code&gt; infix operators, that simplifies considerably to&lt;/p&gt;

&lt;pre&gt;  my $in-range = $lower max $x min $upper;&lt;/pre&gt;

&lt;p&gt;because &lt;code&gt;$lower max $x&lt;/code&gt; returns the larger of the two numbers, and thus clipping to the lower end of the range.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; are infix operators, you can also clip infix:&lt;/p&gt;

&lt;pre&gt; $x max= 0;
 $x min= 10;&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Debugging&quot;
&gt;Debugging&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Perl 5 has Data::Dumper, Perl 6 objects have the &lt;code&gt;.perl&lt;/code&gt; method. Both generate code that reproduces the original data structure as faithfully as possible.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;:$var&lt;/code&gt; generates a Pair (&amp;#34;colonpair&amp;#34;), using the variable name as key (but with sigil stripped). So it&amp;#39;s the same as &lt;code&gt;var =&amp;#62; $var&lt;/code&gt;. &lt;code&gt;note()&lt;/code&gt; writes to the standard error stream, appending a newline. So &lt;code&gt;note :$var.perl&lt;/code&gt; is quick way of obtaining the value of a variable for debugging; purposes, along with its name.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Sorting&quot;
&gt;Sorting&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Like in Perl 5, the &lt;code&gt;sort&lt;/code&gt; built-in can take a function that compares two values, and then sorts according to that comparison. Unlike Perl 5, it&amp;#39;s a bit smarter, and automatically does a transformation for you if the function takes only one argument.&lt;/p&gt;

&lt;p&gt;In general, if you want to compare by a transformed value, in Perl 5 you can do:&lt;/p&gt;

&lt;pre&gt;    # WARNING: Perl 5 code ahead
    my @sorted = sort { transform($a) cmp transform($b) } @values;

    # or the so-called Schwartzian Transform:
    my @sorted = map { $_-&amp;#62;[1] }
                 sort { $a-&amp;#62;[0] cmp $b-&amp;#62;[0] }
                 map { [transform($_), $_] }
                 @values&lt;/pre&gt;

&lt;p&gt;The former solution requires repetitive typing of the transformation, and executes it for each comparison. The second solution avoids that by storing the transformed value along with the original value, but it&amp;#39;s quite a bit of code to write.&lt;/p&gt;

&lt;p&gt;Perl 6 automates the second solution (and a bit more efficient than the naiive Schwartzian transform, by avoiding an array for each value) when the transformation function has arity one, ie accepts one argument only:&lt;/p&gt;

&lt;pre&gt;    my @sorted = sort &amp;#38;transform, @values;&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Mandatory_Attributes&quot;
&gt;Mandatory Attributes&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;The typical way to enforce the presence of an attribute is to check its presence in the constructor - or in all constructors, if there are many.&lt;/p&gt;

&lt;p&gt;That works in Perl 6 too, but it&amp;#39;s easier and safer to require the presence at the level of each attribute:&lt;/p&gt;

&lt;pre&gt;    has $.attr = die &amp;#34;&amp;#39;attr&amp;#39; is mandatory&amp;#34;;&lt;/pre&gt;

&lt;p&gt;This exploits the default value mechanism. When a value is supplied, the code for generating the default value is never executed, and the &lt;code&gt;die&lt;/code&gt; never triggers. If any constructor fails to set it, an exception is thrown.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;N/A&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>Exceptions and control exceptions</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/26-exceptions.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Thu, 09 Jul 2009 09:00:02 +0100</pubDate>
    <description>&lt;!-- 1247130002 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 26 - Exceptions and control exceptions&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    try {
        die &amp;#34;OH NOEZ&amp;#34;;

        CATCH { 
            say &amp;#34;there was an error: $!&amp;#34;;
        }
    }&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Exceptions are, contrary to their name, nothing exceptional. In fact they are part of the normal control flow of programs in Perl 6.&lt;/p&gt;

&lt;p&gt;Exceptions are generated either by implicit errors (for example calling a non-existing method, or type check failures) or by explicitly calling &lt;code&gt;die&lt;/code&gt; or other functions.&lt;/p&gt;

&lt;p&gt;When an exception is thrown, the program searches for &lt;code&gt;CATCH&lt;/code&gt; statements or &lt;code&gt;try&lt;/code&gt; blocks in the caller frames, unwinding the stack all the way (that means it forcibly returns from all routines called so far). If no &lt;code&gt;CATCH&lt;/code&gt; or &lt;code&gt;try&lt;/code&gt; is found, the program terminates, and prints out a hopefully helpful error message. If one was found, the error message is stored in the special variable &lt;code&gt;$!&lt;/code&gt;, and the &lt;code&gt;CATCH&lt;/code&gt; block is executed (or in the case of a &lt;code&gt;try&lt;/code&gt; without a CATCH block the try block returns &lt;code&gt;Any&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;So far exceptions might still sound exceptional, but error handling is integral part of each non-trivial application. But even more, normal &lt;code&gt;return&lt;/code&gt; statements also throw exceptions!&lt;/p&gt;

&lt;p&gt;They are called &lt;i&gt;control exceptions&lt;/i&gt;, and can be caught with &lt;code&gt;CONTROL&lt;/code&gt; blocks, or are implicitly caught at each routine declaration.&lt;/p&gt;

&lt;p&gt;Consider this example:&lt;/p&gt;

&lt;pre&gt;    use v6;

    sub s {
        my $block = -&amp;#62; { return &amp;#34;block&amp;#34;; say &amp;#34;still here&amp;#34; };
        $block();
        return &amp;#34;sub&amp;#34;;
    }

    say s();    # block&lt;/pre&gt;

&lt;p&gt;Here the &lt;code&gt;return &amp;#34;block&amp;#34;&lt;/code&gt; throws a control exception, causing it to not only exit the current block (and thus not printing &lt;code&gt;still here&lt;/code&gt; on the screen), but also exiting the subroutine, where it is caught by the &lt;code&gt;sub s...&lt;/code&gt; declaration. The payload, here a string, is handed back as the return value, and the &lt;code&gt;say&lt;/code&gt; in the last line prints it to the screen.&lt;/p&gt;

&lt;p&gt;Adding a &lt;code&gt;CONTROL { ... }&lt;/code&gt; block to the scope in which &lt;code&gt;$block&lt;/code&gt; is called causes it to catch the control exception.&lt;/p&gt;

&lt;p&gt;Contrary to what other programming languages do, the &lt;code&gt;CATCH&lt;/code&gt;/&lt;code&gt;CONTROL&lt;/code&gt; blocks are within the scope in which the error is caught (not on the outside), giving it full access to the lexical variables, which makes it easier to generate useful error message, and also prevents DESTROY blocks from being run before the error is handled.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Unthrown_exceptions&quot;
&gt;Unthrown exceptions&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Perl 6 embraces the idea of multi threading, and in particular automated parallelization. To make sure that not all threads suffer from the termination of a single thread, a kind of &amp;#34;soft&amp;#34; exception was invented.&lt;/p&gt;

&lt;p&gt;When a function calls &lt;code&gt;fail($obj)&lt;/code&gt;, it returns a special value of &lt;code&gt;undef&lt;/code&gt;, which contains the payload &lt;code&gt;$obj&lt;/code&gt; (usually an error message) and the back trace (file name and line number). Processing that special undefined value without check if it&amp;#39;s undefined causes a normal exception to be thrown.&lt;/p&gt;

&lt;pre&gt;    my @files = &amp;#60;/etc/passwd /etc/shadow nonexisting&amp;#62;;
    my @handles = hyper map { open($_) }, @files; # hyper not yet implement&lt;/pre&gt;

&lt;p&gt;In this example the &lt;code&gt;hyper&lt;/code&gt; operator tells &lt;code&gt;map&lt;/code&gt; to parallelize its actions as far as possible. When the opening of the &lt;code&gt;nonexisting&lt;/code&gt; file fails, an ordinary &lt;code&gt;die &amp;#34;No such file or directory&amp;#34;&lt;/code&gt; would also abort the execution of all other &lt;code&gt;open&lt;/code&gt; operations. But since a failed open calls &lt;code&gt;fail(&amp;#34;No such file or directory&amp;#34;&lt;/code&gt; instead, it gives the caller the possibility to check the contents of &lt;code&gt;@handles&lt;/code&gt;, and it &lt;i&gt;still has access to the full error message&lt;/i&gt;.&lt;/p&gt;

&lt;p&gt;If you don&amp;#39;t like soft exceptions, you say &lt;code&gt;use fatal;&lt;/code&gt; at the start of the program and cause all exceptions from &lt;code&gt;fail()&lt;/code&gt; to be thrown immediately.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;A good programming language needs exceptions to handle error conditions. Always checking return values for success is a plague and easily forgotten.&lt;/p&gt;

&lt;p&gt;Since traditional exceptions can be poisonous for implicit parallelism, we needed a solution that combined the best of both worlds: not killing everything at once, and still not losing any information.&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>The Cross Meta Operator</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/25-cross-meta-operator.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Tue, 26 May 2009 22:00:00 +0100</pubDate>
    <description>&lt;!-- 1243375200 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 25 - The Cross Meta Operator&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    for &amp;#60;a b&amp;#62; X 1..3 -&amp;#62; $a, $b {
        print &amp;#34;$a: $b   &amp;#34;;
    }
    # output: a: 1  a: 2  a: 3  b: 1  b: 2  b: 3

    .say for &amp;#60;a b c&amp;#62; X 1, 2;
    # output: a 1 a 2 b 1 b 2 c 1 c 2
    # (with newlines instead of spaces)&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;The cross operator &lt;code&gt;X&lt;/code&gt; returns the Cartesian product of two or more lists, which means that it returns all possible tuples where the first item is an item of the first list, the second item is an item of second list etc.&lt;/p&gt;

&lt;p&gt;If an operator follows the &lt;code&gt;X&lt;/code&gt;, then this operator is applied to all tuple items, and the result is returned instead. So &lt;code&gt;1, 2 X+ 3, 6&lt;/code&gt; will return the values &lt;code&gt;1+3, 1+6, 2+3, 2+6&lt;/code&gt; (evaluated as &lt;code&gt;4, 7, 5, 8&lt;/code&gt; of course).&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;It&amp;#39;s quite common that one has to iterate over all possible combinations of two or more lists, and the cross operator can condense that into a single iteration, thus simplifying programs and using up one less indentation level.&lt;/p&gt;

&lt;p&gt;The usage as a meta operator can sometimes eliminate the loops altogether.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://design.perl6.org/S03.html#Cross_operators&quot; class=&quot;podlinkurl&quot;
&gt;http://design.perl6.org/S03.html#Cross_operators&lt;/a&gt;,&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>The Reduction Meta Operator</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/24-reduce-meta-operator.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Tue, 09 Dec 2008 23:00:00 +0100</pubDate>
    <description>&lt;!-- 1228863600 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 24 - The Reduction Meta Operator&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    say [+] 1, 2, 3;    # 6
    say [+] ();         # 0
    say [~] &amp;#60;a b&amp;#62;;      # ab
    say [**] 2, 3, 4;   # 2417851639229258349412352

    [\+] 1, 2, 3, 4     # 1, 3, 6, 10
    [\**] 2, 3, 4       # 4, 81, 2417851639229258349412352

    if [&amp;#60;=] @list {
        say &amp;#34;ascending order&amp;#34;;
    }&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;Description&quot;
&gt;Description&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;The reduction meta operator &lt;code&gt;[...]&lt;/code&gt; can enclose any associative infix operator, and turn it into a list operator. This happens as if the operator was just put between the items of the list, so &lt;code&gt;[op] $i1, $i2, @rest&lt;/code&gt; returns the same result as if it was written as &lt;code&gt;$i1 op $i2 op @rest[0] op @rest[1] ...&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is a very powerful construct that promotes the plus &lt;code&gt;+&lt;/code&gt; operator into a &lt;code&gt;sum&lt;/code&gt; function, &lt;code&gt;~&lt;/code&gt; into a &lt;code&gt;join&lt;/code&gt; (with empty separator) and so on. It is somewhat similar to the &lt;code&gt;List.reduce&lt;/code&gt; function, and if you had some exposure to functional programming, you&amp;#39;ll probably know about &lt;code&gt;foldl&lt;/code&gt; and &lt;code&gt;foldr&lt;/code&gt; (in Lisp or Haskell). Unlike those, &lt;code&gt;[...]&lt;/code&gt; respects the associativity of the enclosed operator, so &lt;code&gt;[/] 1, 2, 3&lt;/code&gt; is interpreted as &lt;code&gt;(1 / 2) / 3&lt;/code&gt; (left associative), &lt;code&gt;[**] 1, 2, 3&lt;/code&gt; is handled correctly as &lt;code&gt;1 ** (2**3)&lt;/code&gt; (right associative).&lt;/p&gt;

&lt;p&gt;Like all other operators, whitespace are forbidden, so you while you can write &lt;code&gt;[+]&lt;/code&gt;, you can&amp;#39;t say &lt;code&gt;[ + ]&lt;/code&gt;. (This also helps to disambiguate it from array literals).&lt;/p&gt;

&lt;p&gt;Since comparison operators can be chained, you can also write things like&lt;/p&gt;

&lt;pre&gt;    if    [==] @nums { say &amp;#34;all nums in @nums are the same&amp;#34; }
    elsif [&amp;#60;]  @nums { say &amp;#34;@nums is in strict ascending order&amp;#34; }
    elsif [&amp;#60;=] @nums { say &amp;#34;@nums is in ascending order&amp;#34;}&lt;/pre&gt;

&lt;p&gt;However you cannot reduce the assignment operator:&lt;/p&gt;

&lt;pre&gt;    my @a = 1..3;
    [=] @a, 4;          # Cannot reduce with = because list assignment operators are too fiddly&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Getting_partial_results&quot;
&gt;Getting partial results&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;There&amp;#39;s a special form of this operator that uses a backslash like this: &lt;code&gt;[\+]&lt;/code&gt;. It returns a list of the partial evaluation results. So &lt;code&gt;[\+] 1..3&lt;/code&gt; returns the list &lt;code&gt;1, 1+2, 1+2+3&lt;/code&gt;, which is of course &lt;code&gt;1, 3, 6&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    [\~] &amp;#39;a&amp;#39; .. &amp;#39;d&amp;#39;     # &amp;#60;a ab abc abcd&amp;#62;&lt;/pre&gt;

&lt;p&gt;Since right-associative operators evaluate from right to left, you also get the partial results that way:&lt;/p&gt;

&lt;pre&gt;    [\**] 1..3;         # 3, 2**3, 1**(2**3), which is 3, 8, 1&lt;/pre&gt;

&lt;p&gt;Multiple reduction operators can be combined:&lt;/p&gt;

&lt;pre&gt;    [~] [\**] 1..3;     # &amp;#34;381&amp;#34;&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Programmers are lazy, and don&amp;#39;t want to write a loop just to apply a binary operator to all elements of a list. &lt;code&gt;List.reduce&lt;/code&gt; does something similar, but it&amp;#39;s not as terse as the meta operator (&lt;code&gt;[+] @list&lt;/code&gt; would be &lt;code&gt;@list.reduce(&amp;#38;infix:&amp;#60;+&amp;#62;)&lt;/code&gt;). Also with reduce you have to takes care of the associativity of the operator yourself, whereas the meta operator handles it for you.&lt;/p&gt;

&lt;p&gt;If you&amp;#39;re not convinced, play a bit with it (rakudo implements it), it&amp;#39;s real fun.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://design.perl6.org/S03.html#Reduction_operators&quot; class=&quot;podlinkurl&quot;
&gt;http://design.perl6.org/S03.html#Reduction_operators&lt;/a&gt;, &lt;a href=&quot;http://www.perlmonks.org/?node_id=716497&quot; class=&quot;podlinkurl&quot;
&gt;http://www.perlmonks.org/?node_id=716497&lt;/a&gt;&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>Quoting and Parsing</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/23-quoting-and-parsing.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Mon, 08 Dec 2008 23:00:00 +0100</pubDate>
    <description>&lt;!-- 1228777200 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 23 - Quoting and Parsing&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    my @animals = &amp;#60;dog cat tiger&amp;#62;
    # or
    my @animals = qw/dog cat tiger/;
    # or 
    
    my $interface = q{eth0};
    my $ips = q :s :x /ifconfig $interface/;

    # -----------

    sub if {
        warn &amp;#34;if() calls a sub\n&amp;#34;;
    }
    if();&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Quoting&quot;
&gt;Quoting&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Perl 6 has a powerful mechanism of quoting strings, you have exact control over what features you want in your string.&lt;/p&gt;

&lt;p&gt;Perl 5 had single quotes, double quotes and &lt;code&gt;qw(...)&lt;/code&gt; (single quotes, splitted on whitespaces) as well as the &lt;code&gt;q(..)&lt;/code&gt; and &lt;code&gt;qq(...)&lt;/code&gt; forms which are basically synonyms for single and double quotes.&lt;/p&gt;

&lt;p&gt;Perl 6 in turn defines a quote operator named &lt;code&gt;Q&lt;/code&gt; that can take various modifiers. The &lt;code&gt;:b&lt;/code&gt; (&lt;i&gt;backslash&lt;/i&gt;) modifier allows interpolation of backslash escape sequences like &lt;code&gt;\n&lt;/code&gt;, the &lt;code&gt;:s&lt;/code&gt; modifier allows interpolation of scalar variables, &lt;code&gt;:c&lt;/code&gt; allows the interpolation of closures (&lt;code&gt;&amp;#34;1 + 2 = { 1 + 2 }&amp;#34;&lt;/code&gt;) and so on, &lt;code&gt;:w&lt;/code&gt; splits on words as &lt;code&gt;qw/.../&lt;/code&gt; does.&lt;/p&gt;

&lt;p&gt;You can arbitrarily combine those modifiers. For example you might wish a form of &lt;code&gt;qw/../&lt;/code&gt; that interpolates only scalars, but nothing else? No problem:&lt;/p&gt;

&lt;pre&gt;    my $stuff = &amp;#34;honey&amp;#34;;
    my @list = Q :w :s/milk toast $stuff with\tfunny\nescapes/;
    say @list[*-1];                     # with\nfunny\nescapes&lt;/pre&gt;

&lt;p&gt;Here&amp;#39;s a list of what modifiers are available, mostly stolen from S02 directly. All of these also have long names, which I omitted here.&lt;/p&gt;

&lt;pre&gt;    Features:
        :q          Interpolate \\, \q and \&amp;#39;
        :b          Other backslash escape sequences like \n, \t
    Operations:
        :x          Execute as shell command, return result
        :w          Split on whitespaces
        :ww         Split on whitespaces, with quote protection
    Variable interpolation
        :s          Interpolate scalars   ($stuff)
        :a          Interpolate arrays    (@stuff[])
        :h          Interpolate hashes    (%stuff{})
        :f          Interpolate functions (&amp;#38;stuff())
    Other
        :c          Interpolate closures  ({code})
        :qq         Interpolate with :s, :a, :h, :f, :c, :b
        :regex      parse as regex&lt;/pre&gt;

&lt;p&gt;There are some short forms which make life easier for you:&lt;/p&gt;

&lt;pre&gt;    q       Q:q
    qq      Q:qq
    m       Q:regex&lt;/pre&gt;

&lt;p&gt;You can also omit the first colon &lt;code&gt;:&lt;/code&gt; if the quoting symbol is a short form, and write it as a singe word:&lt;/p&gt;

&lt;pre&gt;    symbol      short for
    qw          q:w
    Qw          Q:w
    qx          q:x
    Qc          Q:c
    # and so on.&lt;/pre&gt;

&lt;p&gt;However there is one form that does not work, and some Perl 5 programmers will miss it: you can&amp;#39;t write &lt;code&gt;qw(...)&lt;/code&gt; with the round parenthesis in Perl 6. It is interpreted as a call to sub &lt;code&gt;qw&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Parsing&quot;
&gt;Parsing&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;This is where parsing comes into play: Every construct of the form &lt;code&gt;identifier(...)&lt;/code&gt; is parsed as sub call. Yes, every.&lt;/p&gt;

&lt;pre&gt;    if($x&amp;#60;3)&lt;/pre&gt;

&lt;p&gt;is parsed as a call to sub &lt;code&gt;if&lt;/code&gt;. You can disambiguate with whitespace:&lt;/p&gt;

&lt;pre&gt;    if ($x &amp;#60; 3) { say &amp;#39;&amp;#60;3&amp;#39; }&lt;/pre&gt;

&lt;p&gt;Or just omit the parens altogether:&lt;/p&gt;

&lt;pre&gt;    if $x &amp;#60; 3 { say &amp;#39;&amp;#60;3&amp;#39; }&lt;/pre&gt;

&lt;p&gt;This implies that Perl 6 has no keywords. Actually there are keywords like &lt;code&gt;use&lt;/code&gt; or &lt;code&gt;if&lt;/code&gt;, but they are not reserved in the sense that identifiers are restricted to non-keywords.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Various combinations of the quoting modifiers are already used internally, for example &lt;code&gt;q:w&lt;/code&gt; to parse &lt;code&gt;&amp;#60;...&amp;#62;&lt;/code&gt;, and &lt;code&gt;:regex&lt;/code&gt; for &lt;code&gt;m/.../&lt;/code&gt;. It makes sense to expose these also to the user, who gains flexibility, and can very easily write macros that provide a shortcut for the exact quoting semantics he wants.&lt;/p&gt;

&lt;p&gt;And when you limit the specialty of keywords, you have far less troubles with backwards compatibility if you want to change what you consider a &amp;#34;keyword&amp;#34;.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://design.perl6.org/S02.html#Literals&quot; class=&quot;podlinkurl&quot;
&gt;http://design.perl6.org/S02.html#Literals&lt;/a&gt;&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>The State of the implementations</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/22-state.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Sun, 07 Dec 2008 23:00:00 +0100</pubDate>
    <description>&lt;!-- 1228690800 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 22 - The State of the implementations&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    (none)&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; This lesson is long outdated, and preserved for historical interest only. The best way to stay informed about various Perl 6 compilers is to follow the blogs at &lt;a href=&quot;http://planetsix.perl.org/&quot; class=&quot;podlinkurl&quot;
&gt;http://planetsix.perl.org/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Perl 6 is a language specification, and multiple compilers are being written that aim to implement Perl 6, and partially they already do.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Pugs&quot;
&gt;Pugs&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Pugs is a Perl 6 compiler written in Haskell. It was started by Audrey Tang, and she also did most of the work. In terms of implemented features it might still be the most advanced implementation today (May 2009).&lt;/p&gt;

&lt;p&gt;To build and test pugs, you have to install GHC 6.10.1 first, and then run&lt;/p&gt;

&lt;pre&gt;    svn co http://svn.pugscode.org/pugs
    cd pugs
    perl Makefile.PL
    make
    make test&lt;/pre&gt;

&lt;p&gt;That will install some Haskell dependencies locally and then build pugs. For &lt;code&gt;make test&lt;/code&gt; you might need to install some Perl 5 modules, which you can do with &lt;code&gt;cpan Task::Smoke&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Pugs hasn&amp;#39;t been developed during the last three years, except occasional clean-ups of the build system.&lt;/p&gt;

&lt;p&gt;Since the specification is evolving and Pugs is not updated, it is slowly drifting into obsoleteness.&lt;/p&gt;

&lt;p&gt;Pugs can parse most common constructs, implements object orientation, basic regexes, nearly(?) all control structures, basic user defined operators and macros, many builtins, contexts (except slice context), junctions, basic multi dispatch and the reduction meta operator - based on the syntax of three years past.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Rakudo&quot;
&gt;Rakudo&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Rakudo is a parrot based compiler for Perl 6. The main architect is Patrick Michaud, many features were implemented by Jonathan Worthington.&lt;/p&gt;

&lt;p&gt;It is hosted on github, you can find build instructions on &lt;a href=&quot;http://rakudo.org/how-to-get-rakudo&quot; class=&quot;podlinkurl&quot;
&gt;http://rakudo.org/how-to-get-rakudo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Rakudo development is very active, it&amp;#39;s the most active Perl 6 compiler today. It passes a bit more than 17,000 tests from the official test suite (July 2009).&lt;/p&gt;

&lt;p&gt;It implements most control structures, most syntaxes for number literals, interpolation of scalars and closures, chained operators, &lt;code&gt;BEGIN&lt;/code&gt;- and &lt;code&gt;END&lt;/code&gt; blocks, pointy blocks, named, optional and slurpy arguments, sophisticated multi dispatch, large parts of the object system, regexes and grammars, Junctions, generic types, parametric roles, typed arrays and hashes, importing and exporting of subroutines and basic meta operators.&lt;/p&gt;

&lt;p&gt;If you want to experiment with Perl 6 today, Rakudo is the recommended choice.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Elf&quot;
&gt;Elf&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Mitchell Charity started &lt;i&gt;elf&lt;/i&gt;, a bootstrapping compiler written in Perl 6, with a grammar written in Ruby. Currently it has a Perl 5 backend, others are in planning.&lt;/p&gt;

&lt;p&gt;It lives in the pugs repository, once you&amp;#39;ve checked it out you can go to &lt;code&gt;misc/elf/&lt;/code&gt; and run &lt;code&gt;./elf_f $filename&lt;/code&gt;. You&amp;#39;ll need ruby-1.9 and some perl modules, about which elf will complain bitterly when they are not present.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;elf&lt;/code&gt; is developed in bursts of activity followed by weeks of low activity, or even none at all.&lt;/p&gt;

&lt;p&gt;It parses more than 70% of the test suite, but implements mostly features that are easy to emulate with Perl 5, and passes about 700 tests from the test suite.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;KindaPerl6&quot;
&gt;KindaPerl6&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Flavio Glock started KindaPerl6 (short kp6), a mostly bootstrapped Perl 6 compiler. Since the bootstrapped version is much too slow to be fun to develop with, it is now waiting for a faster backend.&lt;/p&gt;

&lt;p&gt;Kp6 implements object orientation, grammars and a few distinct features like lazy gather/take. It also implements &lt;code&gt;BEGIN&lt;/code&gt; blocks, which was one of the design goals.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;v6.pm&quot;
&gt;v6.pm&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;v6&lt;/code&gt; is a source filter for Perl 5. It was written by Flavio Glock, and supports basic Perl 6 plus grammars. It is fairly stable and fast, and is occasionally enhanced. It lives on the CPAN and in the pugs repository in &lt;code&gt;perl5/*/&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;SMOP&quot;
&gt;SMOP&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Smop stands for &lt;i&gt;Simple Meta Object Programming&lt;/i&gt; and doesn&amp;#39;t plan to implement all of Perl 6, it is designed as a backend (a little bit like parrot, but very different in both design and feature set). Unlike the other implementations it aims explicitly at implementing Perl 6&amp;#39;s powerful meta object programming facilities, ie the ability to plug in different object systems.&lt;/p&gt;

&lt;p&gt;It is implemented in C and various domain specific languages. It was designed and implemented by Daniel Ruoso, with help from Yuval Kogman (design) and Pawe&amp;#322; Murias (implementation, DSLs). A grant from The Perl Foundation supports its development, and it currently approaches the stage where one could begin to emit code for it from another compiler.&lt;/p&gt;

&lt;p&gt;It will then be used as a backend for either elf or kp6, and perhaps also for pugs.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;STD.pm&quot;
&gt;STD.pm&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Larry Wall wrote a grammar for Perl 6 in Perl 6. He also wrote a cheating script named &lt;code&gt;gimme5&lt;/code&gt;, which translates that grammar to Perl 5. It can parse about every written and valid piece of Perl 6 that we know of, including the whole test suite (apart from a few failures now and then when Larry accidentally broke something).&lt;/p&gt;

&lt;p&gt;STD.pm lives in the pugs repository, and can be run and tested with perl-5.10.0 installed in &lt;code&gt;/usr/local/bin/perl&lt;/code&gt; and a few perl modules (like &lt;code&gt;YAML::XS&lt;/code&gt; and &lt;code&gt;Moose&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;    cd src/perl6/
    make
    make testt      # warning: takes lot of time, 80 minutes or so
    ./tryfile $your_file&lt;/pre&gt;

&lt;p&gt;It correctly parses custom operators and warns about non-existent subs, undeclared variables and multiple declarations of the same variable as well as about some Perl 5isms.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Many people ask why we need so many different implementations, and if it wouldn&amp;#39;t be better to focus on one instead.&lt;/p&gt;

&lt;p&gt;There are basically three answers to that.&lt;/p&gt;

&lt;p&gt;Firstly that&amp;#39;s not how programming by volunteers work. People sometimes either want to start something with the tools they like, or they think that one aspect of Perl 6 is not sufficiently honoured by the design of the existing implementations. Then they start a new project.&lt;/p&gt;

&lt;p&gt;The second possible answer is that the projects explore different areas of the vast Perl 6 language: SMOP explores meta object programming (from which Rakudo will also benefit), Rakudo and parrot care a lot about efficient language interoperability, grammars and platform independence, kp6 explored BEGIN blocks, and pugs was the first implementation to explore the syntax, and many parts of the language for the first time.&lt;/p&gt;

&lt;p&gt;The third answer is that we don&amp;#39;t want a single point of failure. If we had just one implementation, and had severe problems with one of them for unforeseeable reasons (technical, legal, personal, ...) we have possible fallbacks.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Pugs: &lt;a href=&quot;http://www.pugscode.org/&quot; class=&quot;podlinkurl&quot;
&gt;http://www.pugscode.org/&lt;/a&gt;, &lt;a href=&quot;http://pugs.blogs.com/pugs/2008/07/pugshs-is-back.html&quot; class=&quot;podlinkurl&quot;
&gt;http://pugs.blogs.com/pugs/2008/07/pugshs-is-back.html&lt;/a&gt;, &lt;a href=&quot;http://pugs.blogspot.com&quot; class=&quot;podlinkurl&quot;
&gt;http://pugs.blogspot.com&lt;/a&gt;, source: &lt;a href=&quot;http://svn.pugscode.org/pugs&quot; class=&quot;podlinkurl&quot;
&gt;http://svn.pugscode.org/pugs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Rakudo: &lt;a href=&quot;http://rakudo.org/&quot; class=&quot;podlinkurl&quot;
&gt;http://rakudo.org/&lt;/a&gt;, &lt;a href=&quot;http://www.parrot.org/&quot; class=&quot;podlinkurl&quot;
&gt;http://www.parrot.org/&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;Elf: &lt;a href=&quot;http://perl.net.au/wiki/Elf&quot; class=&quot;podlinkurl&quot;
&gt;http://perl.net.au/wiki/Elf&lt;/a&gt; source: see pugs, &lt;code&gt;misc/elf/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;KindaPerl6: source: see pugs, &lt;code&gt;v6/v6-KindaPerl6&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;v6.pm: source: see pugs, &lt;code&gt;perl5/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;STD.pm: source: see pugs, &lt;code&gt;src/perl6/&lt;/code&gt;.&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>Subset Types</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/21-subset-types.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Sat, 06 Dec 2008 23:00:00 +0100</pubDate>
    <description>&lt;!-- 1228604400 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 21 - Subset Types&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    subset Squares of Real where { .sqrt.Int**2 == $_ };

    multi sub square_root(Squares $x --&amp;#62; Int) {
        return $x.sqrt.Int;
    }
    multi sub square_root(Real $x --&amp;#62; Real) {
        return $x.sqrt;
    }&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Java programmers tend to think of a type as either a class or an interface (which is something like a crippled class), but that view is too limited for Perl 6. A type is more generally a constraint of what a values a container can constraint. The &amp;#34;classical&amp;#34; constraint is &lt;i&gt;it is an object of a class &lt;code&gt;X&lt;/code&gt; or of a class that inherits from &lt;code&gt;X&lt;/code&gt;&lt;/i&gt;. Perl 6 also has constraints like &lt;i&gt;the class or the object does role &lt;code&gt;Y&lt;/code&gt;&lt;/i&gt;, or &lt;i&gt;this piece of code returns true for our object&lt;/i&gt;. The latter is the most general one, and is called a &lt;i&gt;subset&lt;/i&gt; type:&lt;/p&gt;

&lt;pre&gt;    subset Even of Int where { $_ % 2 == 0 }
    # Even can now be used like every other type name

    my Even $x = 2;
    my Even $y = 3; # type mismatch error&lt;/pre&gt;

&lt;p&gt;(Try it out, Rakudo implements subset types).&lt;/p&gt;

&lt;p&gt;You can also use anonymous subtypes in signatures:&lt;/p&gt;

&lt;pre&gt;    sub foo (Int where { ... } $x) { ... }
    # or with the variable at the front:
    sub foo ($x of Int where { ... } ) { ... }&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Allowing arbitrary type constraints in the form of code allows ultimate extensibility: if you don&amp;#39;t like the current type system, you can just roll your own based on subset types.&lt;/p&gt;

&lt;p&gt;It also makes libraries easier to extend: instead of dying on data that can&amp;#39;t be handled, the subs and methods can simply declare their types in a way that &amp;#34;bad&amp;#34; data is rejected by the multi dispatcher. If somebody wants to handle data that the previous implementation rejected as &amp;#34;bad&amp;#34;, he can simple add a multi sub with the same name that accepts the data. For example a math library that handles real numbers could be enhanced this way to also handle complex numbers.&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>A grammar for (pseudo) XML</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/20-a-grammar-for-xml.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Fri, 05 Dec 2008 23:00:00 +0100</pubDate>
    <description>&lt;!-- 1228518000 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 20 - A grammar for (pseudo) XML&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    grammar XML {
        token TOP   { ^ &amp;#60;xml&amp;#62; $ };
        token xml   { &amp;#60;text&amp;#62; [ &amp;#60;tag&amp;#62; &amp;#60;text&amp;#62; ]* };
        token text {  &amp;#60;-[&amp;#60;&amp;#62;&amp;#38;]&amp;#62;* };
        rule tag   {
            &amp;#39;&amp;#60;&amp;#39;(\w+) &amp;#60;attributes&amp;#62;*
            [
                | &amp;#39;/&amp;#62;&amp;#39;                 # a single tag
                | &amp;#39;&amp;#62;&amp;#39;&amp;#60;xml&amp;#62;&amp;#39;&amp;#60;/&amp;#39; $0 &amp;#39;&amp;#62;&amp;#39;  # an opening and a closing tag
            ]
        };
        token attributes { \w+ &amp;#39;=&amp;#34;&amp;#39; &amp;#60;-[&amp;#34;&amp;#60;&amp;#62;]&amp;#62;* &amp;#39;&amp;#34;&amp;#39; };
    };&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;So far the focus of these articles has been the Perl 6 language, independently of what has been implemented so far. To show you that it&amp;#39;s not a purely fantasy language, and to demonstrate the power of grammars, this lesson shows the development of a grammar that parses basic XML, and that runs with Rakudo.&lt;/p&gt;

&lt;p&gt;Please follow the instructions on &lt;a href=&quot;http://rakudo.org/how-to-get-rakudo/&quot; class=&quot;podlinkurl&quot;
&gt;http://rakudo.org/how-to-get-rakudo/&lt;/a&gt; to obtain and build Rakudo, and try it out yourself.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Our_idea_of_XML&quot;
&gt;Our idea of XML&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;For our purposes XML is quite simple: it consists of plain text and nested tags that can optionally have attributes. So here are few tests for what we want to parse as valid &amp;#34;XML&amp;#34;, and what not:&lt;/p&gt;

&lt;pre&gt;    my @tests = (
        [1, &amp;#39;abc&amp;#39;                       ],      # 1
        [1, &amp;#39;&amp;#60;a&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;                   ],      # 2
        [1, &amp;#39;..&amp;#60;ab&amp;#62;foo&amp;#60;/ab&amp;#62;dd&amp;#39;          ],      # 3
        [1, &amp;#39;&amp;#60;a&amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;           ],      # 4
        [1, &amp;#39;&amp;#60;a href=&amp;#34;foo&amp;#34;&amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;],      # 5
        [1, &amp;#39;&amp;#60;a empty=&amp;#34;&amp;#34; &amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;/a&amp;#62;&amp;#39; ],      # 6
        [1, &amp;#39;&amp;#60;a&amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;c&amp;#62;&amp;#60;/c&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;    ],      # 7
        [0, &amp;#39;&amp;#60;&amp;#39;                         ],      # 8
        [0, &amp;#39;&amp;#60;a&amp;#62;b&amp;#60;/b&amp;#62;&amp;#39;                  ],      # 9
        [0, &amp;#39;&amp;#60;a&amp;#62;b&amp;#60;/a&amp;#39;                   ],      # 10
        [0, &amp;#39;&amp;#60;a&amp;#62;b&amp;#60;/a href=&amp;#34;&amp;#34;&amp;#62;&amp;#39;          ],      # 11
        [1, &amp;#39;&amp;#60;a/&amp;#62;&amp;#39;                      ],      # 12
        [1, &amp;#39;&amp;#60;a /&amp;#62;&amp;#39;                     ],      # 13
    );

    my $count = 1;
    for @tests -&amp;#62; $t {
        my $s = $t[1];
        my $M = XML.parse($s);
        if !($M  xor $t[0]) {
            say &amp;#34;ok $count - &amp;#39;$s&amp;#39;&amp;#34;;
        } else {
            say &amp;#34;not ok $count - &amp;#39;$s&amp;#39;&amp;#34;;
        }
        $count++;
    }&lt;/pre&gt;

&lt;p&gt;This is a list of both &amp;#34;good&amp;#34; and &amp;#34;bad&amp;#34; XML, and a small test script that runs these tests by calling &lt;code&gt;XML.parse($string)&lt;/code&gt;. By convention the rule that matches what the grammar should match is named &lt;code&gt;TOP&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;(As you can see from test 1 we don&amp;#39;t require a single root tag, but it would be trivial to add this restriction).&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Developing_the_grammar&quot;
&gt;Developing the grammar&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;The essence of XML is surely the nesting of tags, so we&amp;#39;ll focus on the second test first. Place this at the top of the test script:&lt;/p&gt;

&lt;pre&gt;    grammar XML {
        token TOP   { ^ &amp;#60;tag&amp;#62; $ }
        token tag   {
            &amp;#39;&amp;#60;&amp;#39; (\w+) &amp;#39;&amp;#62;&amp;#39;
            &amp;#39;&amp;#60;/&amp;#39; $0   &amp;#39;&amp;#62;&amp;#39;
        }
    };&lt;/pre&gt;

&lt;p&gt;Now run the script:&lt;/p&gt;

&lt;pre&gt;    $ ./perl6 xml-01.pl
    not ok 1 - &amp;#39;abc&amp;#39;
    ok 2 - &amp;#39;&amp;#60;a&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    not ok 3 - &amp;#39;..&amp;#60;ab&amp;#62;foo&amp;#60;/ab&amp;#62;dd&amp;#39;
    not ok 4 - &amp;#39;&amp;#60;a&amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    not ok 5 - &amp;#39;&amp;#60;a href=&amp;#34;foo&amp;#34;&amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    not ok 6 - &amp;#39;&amp;#60;a empty=&amp;#34;&amp;#34; &amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    not ok 7 - &amp;#39;&amp;#60;a&amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;c&amp;#62;&amp;#60;/c&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    ok 8 - &amp;#39;&amp;#60;&amp;#39;
    ok 9 - &amp;#39;&amp;#60;a&amp;#62;b&amp;#60;/b&amp;#62;&amp;#39;
    ok 10 - &amp;#39;&amp;#60;a&amp;#62;b&amp;#60;/a&amp;#39;
    ok 11 - &amp;#39;&amp;#60;a&amp;#62;b&amp;#60;/a href=&amp;#34;&amp;#34;&amp;#62;&amp;#39;
    not ok 12 - &amp;#39;&amp;#60;a/&amp;#62;&amp;#39;
    not ok 13 - &amp;#39;&amp;#60;a /&amp;#62;&amp;#39;&lt;/pre&gt;

&lt;p&gt;So this simple rule parses one pair of start tag and end tag, and correctly rejects all four examples of invalid XML.&lt;/p&gt;

&lt;p&gt;The first test should be easy to pass as well, so let&amp;#39;s try this:&lt;/p&gt;

&lt;pre&gt;   grammar XML {
       token TOP   { ^ &amp;#60;xml&amp;#62; $ };
       token xml   { &amp;#60;text&amp;#62; | &amp;#60;tag&amp;#62; };
       token text  { &amp;#60;-[&amp;#60;&amp;#62;&amp;#38;]&amp;#62;*  };
       token tag   {
           &amp;#39;&amp;#60;&amp;#39; (\w+) &amp;#39;&amp;#62;&amp;#39;
           &amp;#39;&amp;#60;/&amp;#39; $0   &amp;#39;&amp;#62;&amp;#39;
       }
    };&lt;/pre&gt;

&lt;p&gt;(Remember, &lt;code&gt;&amp;#60;-[...]&amp;#62;&lt;/code&gt; is a negated character class.)&lt;/p&gt;

&lt;p&gt;And run it:&lt;/p&gt;

&lt;pre&gt;    $ ./perl6 xml-03.pl
    ok 1 - &amp;#39;abc&amp;#39;
    not ok 2 - &amp;#39;&amp;#60;a&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    (rest unchanged)&lt;/pre&gt;

&lt;p&gt;Why in the seven hells did the second test stop working? The answer is that Rakudo doesn&amp;#39;t do longest token matching yet (update 2013-01: it does now), but matches sequentially. &lt;code&gt;&amp;#60;text&amp;#62;&lt;/code&gt; matches the empty string (and thus always), so &lt;code&gt;&amp;#60;text&amp;#62; | &amp;#60;tag&amp;#62;&lt;/code&gt; never even tries to match &lt;code&gt;&amp;#60;tag&amp;#62;&lt;/code&gt;. Reversing the order of the two alternations would help.&lt;/p&gt;

&lt;p&gt;But we don&amp;#39;t just want to match either plain text or a tag anyway, but random combinations of both of them:&lt;/p&gt;

&lt;pre&gt;    token xml   { &amp;#60;text&amp;#62; [ &amp;#60;tag&amp;#62; &amp;#60;text&amp;#62; ]*  };&lt;/pre&gt;

&lt;p&gt;(&lt;code&gt;[...]&lt;/code&gt; are non-capturing groups, like &lt;code&gt;(?: ... )&lt;/code&gt; is in Perl 5).&lt;/p&gt;

&lt;p&gt;And low and behold, the first two tests both pass.&lt;/p&gt;

&lt;p&gt;The third test, &lt;code&gt;..&amp;#60;ab&amp;#62;foo&amp;#60;/ab&amp;#62;dd&lt;/code&gt;, has text between opening and closing tag, so we have to allow that next. But not only text is allowed between tags, but arbitrary XML, so let&amp;#39;s just call &lt;code&gt;&amp;#60;xml&amp;#62;&lt;/code&gt; there:&lt;/p&gt;

&lt;pre&gt;    token tag   {
        &amp;#39;&amp;#60;&amp;#39; (\w+) &amp;#39;&amp;#62;&amp;#39;
        &amp;#60;xml&amp;#62;
        &amp;#39;&amp;#60;/&amp;#39; $0   &amp;#39;&amp;#62;&amp;#39;
    }

    ./perl6 xml-05.pl
    ok 1 - &amp;#39;abc&amp;#39;
    ok 2 - &amp;#39;&amp;#60;a&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    ok 3 - &amp;#39;..&amp;#60;ab&amp;#62;foo&amp;#60;/ab&amp;#62;dd&amp;#39;
    ok 4 - &amp;#39;&amp;#60;a&amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    not ok 5 - &amp;#39;&amp;#60;a href=&amp;#34;foo&amp;#34;&amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    (rest unchanged)&lt;/pre&gt;

&lt;p&gt;We can now focus on attributes (the &lt;code&gt;href=&amp;#34;foo&amp;#34;&lt;/code&gt; stuff):&lt;/p&gt;

&lt;pre&gt;    token tag   {
        &amp;#39;&amp;#60;&amp;#39; (\w+) &amp;#60;attribute&amp;#62;* &amp;#39;&amp;#62;&amp;#39;
        &amp;#60;xml&amp;#62;
        &amp;#39;&amp;#60;/&amp;#39; $0   &amp;#39;&amp;#62;&amp;#39;
    };
    token attribute {
        \w+ &amp;#39;=&amp;#34;&amp;#39; &amp;#60;-[&amp;#34;&amp;#60;&amp;#62;]&amp;#62;* \&amp;#34;
    };&lt;/pre&gt;

&lt;p&gt;But this doesn&amp;#39;t make any new tests pass. The reason is the blank between the tag name and the attribute. Instead of adding &lt;code&gt;\s+&lt;/code&gt; or &lt;code&gt;\s*&lt;/code&gt; in many places we&amp;#39;ll switch from &lt;code&gt;token&lt;/code&gt; to &lt;code&gt;rule&lt;/code&gt;, which implies the &lt;code&gt;:sigspace&lt;/code&gt; modifier:&lt;/p&gt;

&lt;pre&gt;    rule tag   {
        &amp;#39;&amp;#60;&amp;#39;(\w+) &amp;#60;attribute&amp;#62;* &amp;#39;&amp;#62;&amp;#39;
        &amp;#60;xml&amp;#62;
        &amp;#39;&amp;#60;/&amp;#39;$0&amp;#39;&amp;#62;&amp;#39;
    };
    token attribute {
        \w+ &amp;#39;=&amp;#34;&amp;#39; &amp;#60;-[&amp;#34;&amp;#60;&amp;#62;]&amp;#62;* \&amp;#34;
    };&lt;/pre&gt;

&lt;p&gt;Now all tests pass, except the last two:&lt;/p&gt;

&lt;pre&gt;    ok 1 - &amp;#39;abc&amp;#39;
    ok 2 - &amp;#39;&amp;#60;a&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    ok 3 - &amp;#39;..&amp;#60;ab&amp;#62;foo&amp;#60;/ab&amp;#62;dd&amp;#39;
    ok 4 - &amp;#39;&amp;#60;a&amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    ok 5 - &amp;#39;&amp;#60;a href=&amp;#34;foo&amp;#34;&amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    ok 6 - &amp;#39;&amp;#60;a empty=&amp;#34;&amp;#34; &amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    ok 7 - &amp;#39;&amp;#60;a&amp;#62;&amp;#60;b&amp;#62;c&amp;#60;/b&amp;#62;&amp;#60;c&amp;#62;&amp;#60;/c&amp;#62;&amp;#60;/a&amp;#62;&amp;#39;
    ok 8 - &amp;#39;&amp;#60;&amp;#39;
    ok 9 - &amp;#39;&amp;#60;a&amp;#62;b&amp;#60;/b&amp;#62;&amp;#39;
    ok 10 - &amp;#39;&amp;#60;a&amp;#62;b&amp;#60;/a&amp;#39;
    ok 11 - &amp;#39;&amp;#60;a&amp;#62;b&amp;#60;/a href=&amp;#34;&amp;#34;&amp;#62;&amp;#39;
    not ok 12 - &amp;#39;&amp;#60;a/&amp;#62;&amp;#39;
    not ok 13 - &amp;#39;&amp;#60;a /&amp;#62;&amp;#39;&lt;/pre&gt;

&lt;p&gt;These contain un-nested tags that are closed with a single slash &lt;code&gt;/&lt;/code&gt;. No problem to add that to rule &lt;code&gt;tag&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    rule tag   {
        &amp;#39;&amp;#60;&amp;#39;(\w+) &amp;#60;attribute&amp;#62;* [
            | &amp;#39;/&amp;#62;&amp;#39;
            | &amp;#39;&amp;#62;&amp;#39; &amp;#60;xml&amp;#62; &amp;#39;&amp;#60;/&amp;#39;$0&amp;#39;&amp;#62;&amp;#39;
        ]
    };&lt;/pre&gt;

&lt;p&gt;All tests pass, we&amp;#39;re happy, our first grammar works well.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;More_hacking&quot;
&gt;More hacking&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Playing with grammars is much more fun that reading about playing, so here&amp;#39;s what you could implement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;plain text can contain entities like &lt;code&gt;&amp;#38;amp;&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;I don&amp;#39;t know if XML tag names are allowed to begin with a number, but the current grammar allows that. You might look it up in the XML specification, and adapt the grammar if needed.&lt;/li&gt;

&lt;li&gt;plain text can contain &lt;code&gt;&amp;#60;![CDATA[ ... ]]&amp;#62;&lt;/code&gt; blocks, in which xml-like tags are ignored and &lt;code&gt;&amp;#60;&lt;/code&gt; and the like don&amp;#39;t need to be escaped&lt;/li&gt;

&lt;li&gt;Real XML allows a preamble like &lt;code&gt;&amp;#60;?xml version=&amp;#34;0.9&amp;#34; encoding=&amp;#34;utf-8&amp;#34;?&amp;#62;&lt;/code&gt; and requires one root tag which contains the rest (You&amp;#39;d have to change some of the existing test cases)&lt;/li&gt;

&lt;li&gt;You could try to implement a pretty-printer for XML by recursively walking through the match object &lt;code&gt;$/&lt;/code&gt;. (This is non-trivial; you might have to work around a few Rakudo bugs, and maybe also introduce some new captures).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(Please don&amp;#39;t post solutions to this as comments in this blog; let others have the same fun as you had ;-).&lt;/p&gt;

&lt;p&gt;Have fun hacking.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;It&amp;#39;s powerful and fun&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Regexes are specified in great detail in S05: &lt;a href=&quot;http://design.perl6.org/S05.html&quot; class=&quot;podlinkurl&quot;
&gt;http://design.perl6.org/S05.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;More working examples for grammars can be found at &lt;a href=&quot;https://github.com/moritz/json/&quot; class=&quot;podlinkurl&quot;
&gt;https://github.com/moritz/json/&lt;/a&gt; (check file lib/JSON/Tiny/Grammar.pm).&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>Regexes strike back</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/19-regex.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Sat, 29 Nov 2008 23:00:00 +0100</pubDate>
    <description>&lt;!-- 1227999600 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 19 - Regexes strike back&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    # normal matching:
    if &amp;#39;abc&amp;#39; ~~ m/../ {
        say $/;                 # ab
    }

    # match with implicit :sigspace modifier
    if &amp;#39;ab cd ef&amp;#39;  ~~ ms/ (..) ** 2 / {
        say $0[1];              # cd
    }

    # substitute with the :samespace modifier
    my $x = &amp;#34;abc     defg&amp;#34;;
    $x ~~ ss/c d/x y/;
    say $x;                     # abx     yefg&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Since the basics of regexes are already covered in lesson 07, here are some useful (but not very structured) additional facts about Regexes.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Matching&quot;
&gt;Matching&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;You don&amp;#39;t need to write grammars to match regexes, the traditional form &lt;code&gt;m/.../&lt;/code&gt; still works, and has a new brother, the &lt;code&gt;ms/.../&lt;/code&gt; form, which implies the &lt;code&gt;:sigspace&lt;/code&gt; modifier. Remember, that means that whitespaces in the regex are substituted by the &lt;code&gt;&amp;#60;.ws&amp;#62;&lt;/code&gt; rule.&lt;/p&gt;

&lt;p&gt;The default for the rule is to match &lt;code&gt;\s+&lt;/code&gt; if it is surrounded by two word-characters (ie those matching those &lt;code&gt;\w&lt;/code&gt;), and &lt;code&gt;\s*&lt;/code&gt; otherwise.&lt;/p&gt;

&lt;p&gt;In substitutions the &lt;code&gt;:samespace&lt;/code&gt; modifier takes care that whitespaces matched with the &lt;code&gt;ws&lt;/code&gt; rule are preserved. Likewise the &lt;code&gt;:samecase&lt;/code&gt; modifier, short &lt;code&gt;:ii&lt;/code&gt; (since it&amp;#39;s a variant of &lt;code&gt;:i&lt;/code&gt;) preserves case.&lt;/p&gt;

&lt;pre&gt;    my $x = &amp;#39;Abcd&amp;#39;;
    $x ~~ s:ii/^../foo/;
    say $x;                     # Foocd
    $x = &amp;#39;ABC&amp;#39;;
    $x ~~ s:ii/^../foo/;
    say $x                      # FOOC&lt;/pre&gt;

&lt;p&gt;This is very useful if you want to globally rename your module &lt;code&gt;Foo&lt;/code&gt;, to &lt;code&gt;Bar&lt;/code&gt;, but for example in environment variables it is written as all uppercase. With the &lt;code&gt;:ii&lt;/code&gt; modifier the case is automatically preserved.&lt;/p&gt;

&lt;p&gt;It copies case information on a character by character. But there&amp;#39;s also a more intelligent version; when combined with the &lt;code&gt;:sigspace&lt;/code&gt; (short &lt;code&gt;:s&lt;/code&gt;) modifier, it tries to find a pattern in the case information of the source string. Recognized are &lt;code&gt;.lc&lt;/code&gt;, &lt;code&gt;.uc&lt;/code&gt;, &lt;code&gt;.lc.ucfirst&lt;/code&gt;, &lt;code&gt;.uc.lcfirst&lt;/code&gt; and &lt;code&gt;.lc.capitaliz&lt;/code&gt; (&lt;code&gt;Str.capitalize&lt;/code&gt; uppercases the first character of each word). If such a pattern is found, it is also applied to the substitution string.&lt;/p&gt;

&lt;pre&gt;    my $x = &amp;#39;The Quick Brown Fox&amp;#39;;
    $x ~~ s :s :ii /brown.*/perl 6 developer/;
    # $x is now &amp;#39;The Quick Perl 6 Developer&amp;#39;&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Alternations&quot;
&gt;Alternations&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Alternations are still formed with the single bar &lt;code&gt;|&lt;/code&gt;, but it means something else than in Perl 5. Instead of sequentially matching the alternatives and taking the first match, it now matches all alternatives in parallel, and takes the longest one.&lt;/p&gt;

&lt;pre&gt;    &amp;#39;aaaa&amp;#39; ~~ m/ a | aaa | aa /;
    say $/                          # aaa&lt;/pre&gt;

&lt;p&gt;While this might seem like a trivial change, it has far reaching consequences, and is crucial for extensible grammars. Since Perl 6 is parsed using a Perl 6 grammar, it is responsible for the fact that in &lt;code&gt;++$a&lt;/code&gt; the &lt;code&gt;++&lt;/code&gt; is parsed as a single token, not as two &lt;code&gt;prefix:&amp;#60;+&amp;#62;&lt;/code&gt; tokens.&lt;/p&gt;

&lt;p&gt;The old, sequential style is still available with &lt;code&gt;||&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    grammar Math::Expression {
        token value {
            | &amp;#60;number&amp;#62;
            | &amp;#39;(&amp;#39; 
              &amp;#60;expression&amp;#62; 
              [ &amp;#39;)&amp;#39; || { fail(&amp;#34;Parenthesis not closed&amp;#34;) } ]
        }

        ...
    }&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;{ ... }&lt;/code&gt; execute a closure, and calling &lt;code&gt;fail&lt;/code&gt; in that closure makes the expression fail. That branch is guaranteed to be executed only if the previous (here the &lt;code&gt;&amp;#39;)&amp;#39;&lt;/code&gt;) fails, so it can be used to emit useful error messages while parsing.&lt;/p&gt;

&lt;p&gt;There are other ways to write alternations, for example if you &amp;#34;interpolate&amp;#34; an array, it will match as an alternation of its values:&lt;/p&gt;

&lt;pre&gt;    $_ = &amp;#39;12 oranges&amp;#39;;
    my @fruits = &amp;#60;apple orange banana kiwi&amp;#62;;
    if m:i:s/ (\d+) (@fruits)s? / {
        say &amp;#34;You&amp;#39;ve got $0 $1s, I&amp;#39;ve got { $0 + 2 } of them. You lost.&amp;#34;;
    }&lt;/pre&gt;

&lt;p&gt;There is yet another construct that automatically matches the longest alternation: multi regexes. They can be either written as &lt;code&gt;multi token name&lt;/code&gt; or with a &lt;code&gt;proto&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    grammar Perl {
        ...
        proto token sigil { * }
        token sigil:sym&amp;#60;$&amp;#62; { &amp;#60;sym&amp;#62; }
        token sigil:sym&amp;#60;@&amp;#62; { &amp;#60;sym&amp;#62; }
        token sigil:sym&amp;#60;%&amp;#62; { &amp;#60;sym&amp;#62; }
        ...

       token variable { &amp;#60;sigil&amp;#62; &amp;#60;twigil&amp;#62;? &amp;#60;identifier&amp;#62; }
   }&lt;/pre&gt;

&lt;p&gt;This example shows multiple tokens called &lt;code&gt;sigil&lt;/code&gt;, which are parameterized by &lt;code&gt;sym&lt;/code&gt;. When the short name, ie &lt;code&gt;sigil&lt;/code&gt; is used, all of these tokens are matched in an alternation. You may think that this is a very inconvenient way to write an alternation, but it has a huge advantage over writing &lt;code&gt;&amp;#39;$&amp;#39;|&amp;#39;@&amp;#39;|&amp;#39;%&amp;#39;&lt;/code&gt;: it is easily extensible:&lt;/p&gt;

&lt;pre&gt;    grammar AddASigil is Perl {
        token sigil:sym&amp;#60;!&amp;#62; { &amp;#60;sym&amp;#62; }
    }
    # wow, we have a Perl 6 grammar with an additional sigil!&lt;/pre&gt;

&lt;p&gt;Likewise you can override existing alternatives:&lt;/p&gt;

&lt;pre&gt;    grammar WeirdSigil is Perl {
        token sigil:sym&amp;#60;$&amp;#62; { &amp;#39;&amp;#176;&amp;#39; }
    }&lt;/pre&gt;

&lt;p&gt;In this grammar the sigil for scalar variables is &lt;code&gt;&amp;#176;&lt;/code&gt;, so whenever the grammar looks for a sigil it searches for a &lt;code&gt;&amp;#176;&lt;/code&gt; instead of a &lt;code&gt;$&lt;/code&gt;, but the compiler will still know that it was the regex &lt;code&gt;sigil:sym&amp;#60;$&amp;#62;&lt;/code&gt; that matched it.&lt;/p&gt;

&lt;p&gt;In the next lesson you&amp;#39;ll see the development of a real, working grammar with Rakudo.&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>Scoping</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/18-scoping.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Fri, 28 Nov 2008 23:00:00 +0100</pubDate>
    <description>&lt;!-- 1227913200 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 18 - Scoping&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    for 1 .. 10 -&amp;#62; $a {
        # $a visible here
    }
    # $a not visible here

    while my $b = get_stuff() {
        # $b visible here
    }
    # $b still visible here

    my $c = 5;
    {
        my $c = $c;
        # $c is undef here
    }
    # $c is 5 here

    my $y;
    my $x = $y + 2 while $y = calc();
    # $x still visible&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Lexical_Scoping&quot;
&gt;Lexical Scoping&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Scoping in Perl 6 is quite similar to that of Perl 5. A Block introduces a new lexical scope. A variable name is searched in the innermost lexical scope first, if it&amp;#39;s not found it is then searched for in the next outer scope and so on. Just like in Perl 5 a &lt;code&gt;my&lt;/code&gt; variable is a proper lexical variable, and an &lt;code&gt;our&lt;/code&gt; declaration introduces a lexical alias for a package variable.&lt;/p&gt;

&lt;p&gt;But there are subtle differences: variables are exactly visible in the rest of the block where they are declared, variables declared in block headers (for example in the condition of a &lt;code&gt;while&lt;/code&gt; loop) are not limited to the block afterwards.&lt;/p&gt;

&lt;p&gt;Also Perl 6 only ever looks up unqualified names (variables and subroutines) in lexical scopes.&lt;/p&gt;

&lt;p&gt;If you want to limit the scope, you can use formal parameters to the block:&lt;/p&gt;

&lt;pre&gt;    if calc() -&amp;#62; $result {
        # you can use $result here
    }
    # $result not visible here&lt;/pre&gt;

&lt;p&gt;Variables are visible immediately after they are declared, not at the end of the statement as in Perl 5.&lt;/p&gt;

&lt;pre&gt;    my $x = .... ;
            ^^^^^
            $x visible here in Perl 6
            but not in Perl 5&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Dynamic_scoping&quot;
&gt;Dynamic scoping&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;local&lt;/code&gt; adjective is now called &lt;code&gt;temp&lt;/code&gt;, and if it&amp;#39;s not followed by an initialization the previous value of that variable is used (not &lt;code&gt;undef&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;There&amp;#39;s also a new kind of dynamically scoped variable called a &lt;i&gt;hypothetical&lt;/i&gt; variable. If the block is left with an exception or a false value,, then the previous value of the variable is restored. If not, it is kept:&lt;/p&gt;

&lt;pre&gt;    use v6;

    my $x = 0;

    sub tryit($success) {
        let $x = 42;
        die &amp;#34;Not like this!&amp;#34; unless $success;
        return True;
    }

    tryit True;
    say $x;             # 42

    $x = 0;
    try tryit False;
    say $x;             # 0&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Context_variables&quot;
&gt;Context variables&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Some variables that are global in Perl 5 (&lt;code&gt;$!&lt;/code&gt;, &lt;code&gt;$_&lt;/code&gt;) are &lt;i&gt;context&lt;/i&gt; variables in Perl 6, that is they are passed between dynamic scopes.&lt;/p&gt;

&lt;p&gt;This solves an old Problem in Perl 5. In Perl 5 an &lt;code&gt;DESTROY&lt;/code&gt; sub can be called at a block exit, and accidentally change the value of a global variable, for example one of the error variables:&lt;/p&gt;

&lt;pre&gt;   # Broken Perl 5 code here:
   sub DESTROY { eval { 1 }; }
   
   eval {
       my $x = bless {};
       die &amp;#34;Death\n&amp;#34;;
   };
   print $@ if $@;         # No output here&lt;/pre&gt;

&lt;p&gt;In Perl 6 this problem is avoided by not implicitly using global variables.&lt;/p&gt;

&lt;p&gt;(In Perl 5.14 there is a workaround that protects &lt;code&gt;$@&lt;/code&gt; from being modified, thus averting the most harm from this particular example.)&lt;/p&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Pseudo-packages&quot;
&gt;Pseudo-packages&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;If a variable is hidden by another lexical variable of the same name, it can be accessed with the &lt;code&gt;OUTER&lt;/code&gt; pseudo package&lt;/p&gt;

&lt;pre&gt;    my $x = 3;
    {
        my $x = 10;
        say $x;             # 10
        say $OUTER::x;      # 3
        say OUTER::&amp;#60;$x&amp;#62;     # 3
    }&lt;/pre&gt;

&lt;p&gt;Likewise a function can access variables from its caller with the &lt;code&gt;CALLER&lt;/code&gt; and &lt;code&gt;CONTEXT&lt;/code&gt; pseudo packages. The difference is that &lt;code&gt;CALLER&lt;/code&gt; only accesses the scope of the immediate caller, &lt;code&gt;CONTEXT&lt;/code&gt; works like UNIX environment variables (and should only be used internally by the compiler for handling &lt;code&gt;$_&lt;/code&gt;, &lt;code&gt;$!&lt;/code&gt; and the like). To access variables from the outer dynamic scope they must be declared with &lt;code&gt;is context&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;It is now common knowledge that global variables are really bad, and cause lots of problems. We also have the resources to implement better scoping mechanism. Therefore global variables are only used for inherently global data (like &lt;code&gt;%*ENV&lt;/code&gt; or &lt;code&gt;$*PID&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The block scoping rules haven been greatly simplified.&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s a quote from Perl 5&amp;#39;s &lt;code&gt;perlsyn&lt;/code&gt; document; we don&amp;#39;t want similar things in Perl 6:&lt;/p&gt;

&lt;pre&gt; NOTE: The behaviour of a &amp;#34;my&amp;#34; statement modified with a statement
 modifier conditional or loop construct (e.g. &amp;#34;my $x if ...&amp;#34;) is
 undefined.  The value of the &amp;#34;my&amp;#34; variable may be &amp;#34;undef&amp;#34;, any
 previously assigned value, or possibly anything else.  Don&amp;#39;t rely on
 it.  Future versions of perl might do something different from the
 version of perl you try it out on.  Here be dragons.&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;S04 discusses block scoping: &lt;a href=&quot;http://design.perl6.org/S04.html&quot; class=&quot;podlinkurl&quot;
&gt;http://design.perl6.org/S04.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;S02 lists all pseudo packages and explains context scoping: &lt;a href=&quot;http://design.perl6.org/S02.html#Names&quot; class=&quot;podlinkurl&quot;
&gt;http://design.perl6.org/S02.html#Names&lt;/a&gt;.&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>Unicode</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/17-unicode.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Thu, 27 Nov 2008 23:00:00 +0100</pubDate>
    <description>&lt;!-- 1227826800 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 17 - Unicode&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;  (none)    &lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Perl 5&amp;#39;s Unicode model suffers from a big weakness: it uses the same type for binary and for text data. For example if your program reads 512 bytes from a network socket, it is certainly a byte string. However when (still in Perl 5) you call &lt;code&gt;uc&lt;/code&gt; on that string, it will be treated as text. The recommended way is to decode that string first, but when a subroutine receives a string as an argument, it can never surely know if it had been encoded or not, ie if it is to be treated as a blob or as a text.&lt;/p&gt;

&lt;p&gt;Perl 6 on the other hand offers the type &lt;code&gt;buf&lt;/code&gt;, which is just a collection of bytes, and &lt;code&gt;Str&lt;/code&gt;, which is a collection of logical characters.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Logical character&lt;/i&gt; is still a vague term. To be more precise a &lt;code&gt;Str&lt;/code&gt; is an object that can be viewed at different levels: &lt;code&gt;Byte&lt;/code&gt;, &lt;code&gt;Codepoint&lt;/code&gt; (anything that the Unicode Consortium assigned a number to is a codepoint), &lt;code&gt;Grapheme&lt;/code&gt; (things that visually appear as a character) and &lt;code&gt;CharLingua&lt;/code&gt; (language defined characters).&lt;/p&gt;

&lt;p&gt;For example the string with the hex bytes &lt;code&gt;61 cc 80&lt;/code&gt; consists of three bytes (obviously), but can also be viewed as being consisting of two codepoints with the names &lt;code&gt;LATIN SMALL LETTER A&lt;/code&gt; (U+0041) and &lt;code&gt;COMBINING GRAVE ACCENT&lt;/code&gt; (U+0300), or as one grapheme that, if neither my blog software nor your browser kill it, looks like this: &lt;code&gt;&amp;#224;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So you can&amp;#39;t simply ask for the length of a string, you have to ask for a specific length:&lt;/p&gt;

&lt;pre&gt;  $str.bytes;
  $str.codes;
  $str.graphs;&lt;/pre&gt;

&lt;p&gt;There&amp;#39;s also method named &lt;code&gt;chars&lt;/code&gt;, which returns the length in the current Unicode level (which can be set by a pragma like &lt;code&gt;use bytes&lt;/code&gt;, and which defaults to graphemes).&lt;/p&gt;

&lt;p&gt;In Perl 5 you sometimes had the problem of accidentally concatenating byte strings and text strings. If you should ever suffer from that problem in Perl 6, you can easily identify where it happens by overloading the concatenation operator:&lt;/p&gt;

&lt;pre&gt;  sub GLOBAL::infix:&amp;#60;~&amp;#62; is deep (Str $a, buf $b)|(buf $b, Str $a) {
      die &amp;#34;Can&amp;#39;t concatenate text string &amp;#171;&amp;#34;
          ~ $a.encode(&amp;#34;UTF-8&amp;#34;)
            &amp;#34;&amp;#187; with byte string &amp;#171;$b&amp;#187;\n&amp;#34;;
  }&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Encoding_and_Decoding&quot;
&gt;Encoding and Decoding&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;The specification of the IO system is very basic and does not yet define any encoding and decoding layers, which is why this article has no useful SYNOPSIS section. I&amp;#39;m sure that there will be such a mechanism, and I could imagine it will look something like this:&lt;/p&gt;

&lt;pre&gt;  my $handle = open($filename, :r, :encoding&amp;#60;UTF-8&amp;#62;);&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u'
name=&quot;Regexes_and_Unicode&quot;
&gt;Regexes and Unicode&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Regexes can take modifiers that specify their Unicode level, so &lt;code&gt;m:codes/./&lt;/code&gt; will match exactly one codepoint. In the absence of such modifiers the current Unicode level will be used.&lt;/p&gt;

&lt;p&gt;Character classes like &lt;code&gt;\w&lt;/code&gt; (match a word character) behave accordingly to the Unicode standard. There are modifiers that ignore case (&lt;code&gt;:i&lt;/code&gt;) and accents (&lt;code&gt;:a&lt;/code&gt;), and modifiers for the substitution operators that can carry case information to the substitution string (&lt;code&gt;:samecase&lt;/code&gt; and &lt;code&gt;:sameaccent&lt;/code&gt;, short &lt;code&gt;:ii&lt;/code&gt;, &lt;code&gt;:aa&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;It is quite hard to correctly process strings with most tools and most programming languages these days. Suppose you have a web application in perl 5, and you want to break long words automatically so that they don&amp;#39;t mess up your layout. When you use naive &lt;code&gt;substr&lt;/code&gt; to do that, you might accidentally rip graphemes apart.&lt;/p&gt;

&lt;p&gt;Perl 6 will be the first mainstream programming language with built in support for grapheme level string manipulation, which basically removes most Unicode worries, and which (in conjunction with regexes) makes Perl 6 one of the most powerful languages for string processing.&lt;/p&gt;

&lt;p&gt;The separate data types for text and byte strings make debugging and introspection quite easy.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://design.perl6.org/S32/Str.html&quot; class=&quot;podlinkurl&quot;
&gt;http://design.perl6.org/S32/Str.html&lt;/a&gt;&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>Enums</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/16-enums.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Wed, 26 Nov 2008 23:00:00 +0100</pubDate>
    <description>&lt;!-- 1227740400 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 16 - Enums&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;  enum Bool &amp;#60;False True&amp;#62;;
  my $value = $arbitrary_value but True;
  if $value {
      say &amp;#34;Yes, it&amp;#39;s true&amp;#34;;       # will be printed
  }

  enum Day (&amp;#39;Mon&amp;#39;, &amp;#39;Tue&amp;#39;, &amp;#39;Wed&amp;#39;, &amp;#39;Thu&amp;#39;, &amp;#39;Fri&amp;#39;, &amp;#39;Sat&amp;#39;, &amp;#39;Sun&amp;#39;);
  if custom_get_date().Day == Day::Sat | Day::Sun {
      say &amp;#34;Weekend&amp;#34;;
  }&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Enums are versatile beasts. They are low-level classes that consist of an enumeration of constants, typically integers or strings (but can be arbitrary).&lt;/p&gt;

&lt;p&gt;These constants can act as subtypes, methods or normal values. They can be attached to an object with the &lt;code&gt;but&lt;/code&gt; operator, which &amp;#34;mixes&amp;#34; the enum into the value:&lt;/p&gt;

&lt;pre&gt;  my $x = $today but Day::Tue;&lt;/pre&gt;

&lt;p&gt;You can also use the type name of the Enum as a function, and supply the value as an argument:&lt;/p&gt;

&lt;pre&gt;  $x = $today but Day($weekday);&lt;/pre&gt;

&lt;p&gt;Afterwards that object has a method with the name of the enum type, here &lt;code&gt;Day&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;  say $x.Day;             # 1&lt;/pre&gt;

&lt;p&gt;The value of first constant is 0, the next 1 and so on, unless you explicitly provide another value with pair notation:&lt;/p&gt;

&lt;pre&gt;  enum Hackers (:Larry&amp;#60;Perl&amp;#62;, :Guido&amp;#60;Python&amp;#62;, :Paul&amp;#60;Lisp&amp;#62;);&lt;/pre&gt;

&lt;p&gt;You can check if a specific value was mixed in by using the versatile smart match operator, or with &lt;code&gt;.does&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;  if $today ~~ Day::Fri {
      say &amp;#34;Thank Christ it&amp;#39;s Friday&amp;#34;
  }
  if $today.does(Fri) { ... }&lt;/pre&gt;

&lt;p&gt;Note that you can specify the name of the value only (like &lt;code&gt;Fri&lt;/code&gt;) if that&amp;#39;s unambiguous, if it&amp;#39;s ambiguous you have to provide the full name &lt;code&gt;Day::Fri&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Enums replace both the &amp;#34;magic&amp;#34; that is involved with tainted variables in Perl 5 and the &lt;code&gt;return &amp;#34;0 but True&amp;#34;&lt;/code&gt; hack (a special case for which no warning is emitted if used as a number). Plus they give a &lt;code&gt;Bool&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;Enums also provide the power and flexibility of attaching arbitrary meta data for debugging or tracing.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://design.perl6.org/S12.html#Enumerations&quot; class=&quot;podlinkurl&quot;
&gt;http://design.perl6.org/S12.html#Enumerations&lt;/a&gt;&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>Twigils</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/15-twigils.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Mon, 20 Oct 2008 22:00:00 +0100</pubDate>
    <description>&lt;!-- 1224540000 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 15 - Twigils&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;  class Foo {
      has $.bar;
      has $!baz;
  }

  my @stuff = sort { $^b[1] &amp;#60;=&amp;#62; $^a[1]}, [1, 2], [0, 3], [4, 8];
  my $block = { say &amp;#34;This is the named &amp;#39;foo&amp;#39; parameter: $:foo&amp;#34; };
  $block(:foo&amp;#60;bar&amp;#62;);

  say &amp;#34;This is file $?FILE on line $?LINE&amp;#34;

  say &amp;#34;A CGI script&amp;#34; if %*ENV&amp;#60;DOCUMENT_ROOT&amp;#62;:exists;&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Some variables have a second sigil, called &lt;i&gt;twigil&lt;/i&gt;. It basically means that the variable isn&amp;#39;t &amp;#34;normal&amp;#34;, but differs in some way, for example it could be differently scoped.&lt;/p&gt;

&lt;p&gt;You&amp;#39;ve already seen that public and private object attributes have the &lt;code&gt;.&lt;/code&gt; and &lt;code&gt;!&lt;/code&gt; twigil respectively; they are not normal variables, they are tied to &lt;code&gt;self&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;^&lt;/code&gt; twigil removes a special case from perl 5. To be able to write&lt;/p&gt;

&lt;pre&gt;  # beware: perl 5 code
  sort { $a &amp;#60;=&amp;#62; $b } @array&lt;/pre&gt;

&lt;p&gt;the variables &lt;code&gt;$a&lt;/code&gt; and &lt;code&gt;$b&lt;/code&gt; are special cased by the &lt;code&gt;strict&lt;/code&gt; pragma. In Perl 6, there&amp;#39;s a concept named &lt;i&gt;self-declared positional parameter&lt;/i&gt;, and these parameters have the &lt;code&gt;^&lt;/code&gt; twigil. It means that they are positional parameters of the current block, without being listed in a signature. The variables are filled in lexicographic (alphabetic) order:&lt;/p&gt;

&lt;pre&gt;  my $block = { say &amp;#34;$^c $^a $^b&amp;#34; };
  $block(1, 2, 3);                # 3 1 2&lt;/pre&gt;

&lt;p&gt;So now you can write&lt;/p&gt;

&lt;pre&gt;  @list = sort { $^b &amp;#60;=&amp;#62; $^a }, @list;
  # or:
  @list = sort { $^foo &amp;#60;=&amp;#62; $^bar }, @list;&lt;/pre&gt;

&lt;p&gt;Without any special cases.&lt;/p&gt;

&lt;p&gt;And to keep the symmetry between positional and named arguments, the &lt;code&gt;:&lt;/code&gt; twigil does the same for named parameters, so these lines are roughly equivalent:&lt;/p&gt;

&lt;pre&gt;  my $block = { say $:stuff }
  my $sub   = sub (:$stuff) { say $stuff }&lt;/pre&gt;

&lt;p&gt;Using both &lt;i&gt;self-declared parameters&lt;/i&gt; and a signature will result in an error, as you can only have one of the two.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;?&lt;/code&gt; twigil stands for variables and constants that are known at compile time, like &lt;code&gt;$?LINE&lt;/code&gt; for the current line number (formerly &lt;code&gt;__LINE__&lt;/code&gt;), and &lt;code&gt;$?DATA&lt;/code&gt; is the file handle to the &lt;code&gt;DATA&lt;/code&gt; section.&lt;/p&gt;

&lt;p&gt;Contextual variables can be accessed with the &lt;code&gt;*&lt;/code&gt; twigil, so &lt;code&gt;$*IN&lt;/code&gt; and &lt;code&gt;$*OUT&lt;/code&gt; can be overridden dynamically.&lt;/p&gt;

&lt;p&gt;A pseudo twigil is &lt;code&gt;&amp;#60;&lt;/code&gt;, which is used in a construct like &lt;code&gt;$&amp;#60;capture&amp;#62;&lt;/code&gt;, where it is a shorthand for &lt;code&gt;$/&amp;#60;capture&amp;#62;&lt;/code&gt;, which accesses the Match object after a regex match.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;When you read Perl 5&amp;#39;s &lt;code&gt;perlvar&lt;/code&gt; document, you can see that it has far too many variables, most of them global, that affect your program in various ways.&lt;/p&gt;

&lt;p&gt;The twigils try to bring some order in these special variables, and at the other hand they remove the need for special cases. In the case of object attributes they shorten &lt;code&gt;self.var&lt;/code&gt; to &lt;code&gt;$.var&lt;/code&gt; (or &lt;code&gt;@.var&lt;/code&gt; or whatever).&lt;/p&gt;

&lt;p&gt;So all in all the increased &amp;#34;punctuation noise&amp;#34; actually makes the programs much more consistent and readable.&lt;/p&gt;

 </description>
  </item>
  <item>
 <title>The MAIN sub</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/14-main-sub.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Sun, 19 Oct 2008 22:00:00 +0100</pubDate>
    <description>&lt;!-- 1224453600 --&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 14 - The MAIN sub&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;  # file doit.pl

  #!/usr/bin/perl6
  sub MAIN($path, :$force, :$recursive, :$home = &amp;#39;~/&amp;#39;) {
      # do stuff here
  }

  # command line
  $ ./doit.pl --force --home=/home/someoneelse file_to_process&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Calling subs and running a typical Unix program from the command line is visually very similar: you can have positional, optional and named arguments.&lt;/p&gt;

&lt;p&gt;You can benefit from it, because Perl 6 can process the command line for you, and turn it into a sub call. Your script is normally executed (at which time it can munge the command line arguments stored in &lt;code&gt;@*ARGS&lt;/code&gt;), and then the sub &lt;code&gt;MAIN&lt;/code&gt; is called, if it exists.&lt;/p&gt;

&lt;p&gt;If the sub can&amp;#39;t be called because the command line arguments don&amp;#39;t match the formal parameters of the &lt;code&gt;MAIN&lt;/code&gt; sub, an automatically generated usage message is printed.&lt;/p&gt;

&lt;p&gt;Command line options map to subroutine arguments like this:&lt;/p&gt;

&lt;pre&gt;  -name                   :name
  -name=value             :name&amp;#60;value&amp;#62;

  # remember, &amp;#60;...&amp;#62; is like qw(...)
  --hackers=Larry,Damian  :hackers&amp;#60;Larry Damian&amp;#62;  

  --good_language         :good_language
  --good_lang=Perl        :good_lang&amp;#60;Perl&amp;#62;
  --bad_lang PHP          :bad_lang&amp;#60;PHP&amp;#62;

  +stuff                  :!stuff
  +stuff=healthy                                        :stuff&amp;#60;healthy&amp;#62; but False&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;$x = $obj but False&lt;/code&gt; means that &lt;code&gt;$x&lt;/code&gt; is a copy of &lt;code&gt;$obj&lt;/code&gt;, but gives &lt;code&gt;Bool::False&lt;/code&gt; in boolean context.&lt;/p&gt;

&lt;p&gt;So for simple (and some not quite simple) cases you don&amp;#39;t need an external command line processor, but you can just use sub &lt;code&gt;MAIN&lt;/code&gt; for that.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;The motivation behind this should be quite obvious: it makes simple things easier, similar things similar, and in many cases reduces command line processing to a single line of code: the signature of &lt;code&gt;MAIN&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://design.perl6.org/S06.html#Declaring_a_MAIN_subroutine&quot; class=&quot;podlinkurl&quot;
&gt;http://design.perl6.org/S06.html#Declaring_a_MAIN_subroutine&lt;/a&gt; contains the specification.&lt;/p&gt;

 </description>
  </item>
 
</channel></rss>