<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<!-- name="generator" content="blosxom/2.0" -->
<channel  rdf:about="http://perlgeek.de/blog-en/">
    <title>Perlgeek.de   </title>
    <link>http://perlgeek.de/blog-en/</link>
    <description>Perl and Programming Blog.</description>
</channel>

  <item rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/26-exceptions.html">
 <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, Jul  9 11:00:02 2009</pubDate>
    <description>&lt;!-- 1247130002 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    try {
        my $x = 1 / 0; # dies with &amp;#39;Divide by zero&amp;#39;

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

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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 dividing by zero, calling a non-existing method, 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;undef&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;
    my $block = -&amp;#62; { return &amp;#34;block&amp;#34;; say &amp;#34;still here&amp;#34; };

    sub s {
        $block.();
        return &amp;#34;sub&amp;#34;;
    }

    say s();&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;Embedding the call &lt;code&gt;$block.()&lt;/code&gt; in a &lt;code&gt;try { ... }&lt;/code&gt; block or adding a &lt;code&gt;CONTROL { ... }&lt;/code&gt; block to the body of the routine causes it to catch the 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' href='#___top' title='click to go to top of document'
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;&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' href='#___top' title='click to go to top of document'
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 rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/25-cross-meta-operator.html">
 <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>Wed, May 27 00:00:00 2009</pubDate>
    <description>&lt;!-- 1243375200 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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: a1\n a2\n b1\n b2\n c1\n c2\n
    # (with real newlines instead of \n)&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

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

 </description>
  </item>
  <item rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/24-reduce-meta-operator.html">
 <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>Wed, Dec 10 00:00:00 2008</pubDate>
    <description>&lt;!-- 1228863600 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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 whitespaces 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;.&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;You can even reduce the assignment operator:&lt;/p&gt;

&lt;pre&gt;    my @a = 1..3;
    [=] @a, 4;          # same as @a[0] = @a[1] = @a[2] = 4;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;[...]&lt;/code&gt; always returns a scalar, so &lt;code&gt;[,] @list&lt;/code&gt; is actually the same as &lt;code&gt;[@list]&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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;), and takes care of the associativity of the operator.&lt;/p&gt;

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

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://perlcabal.org/syn/S03.html#Reduction_operators&quot; class=&quot;podlinkurl&quot;
&gt;http://perlcabal.org/syn/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 rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/23-quoting-and-parsing.html">
 <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>Tue, Dec  9 00:00:00 2008</pubDate>
    <description>&lt;!-- 1228777200 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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];                     # prints 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 proection
    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' href='#___top' title='click to go to top of document'
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 whitespaces:&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 only special if used in a special syntax.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

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

 </description>
  </item>
  <item rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/22-state.html">
 <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>Mon, Dec  8 00:00:00 2008</pubDate>
    <description>&lt;!-- 1228690800 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

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

&lt;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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). Its progress in the test suite is monitored on &lt;a href=&quot;http://rakudo.org/status&quot; class=&quot;podlinkurl&quot;
&gt;http://rakudo.org/status&lt;/a&gt; .&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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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 implements 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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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://www.rakudo.org/&quot; class=&quot;podlinkurl&quot;
&gt;http://www.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 rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/21-subset-types.html">
 <title>Subset Types</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/21-subset-types.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Sun, Dec  7 00:00:00 2008</pubDate>
    <description>&lt;!-- 1228604400 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

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

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

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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 missmatch error&lt;/pre&gt;

&lt;p&gt;(Try it out, Rakudo implements subset types already, but not yet mulit dispatch based on 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' href='#___top' title='click to go to top of document'
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 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 simple 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 rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/20-a-grammar-for-xml.html">
 <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>Sat, Dec  6 00:00:00 2008</pubDate>
    <description>&lt;!-- 1228518000 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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 you 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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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, 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 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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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://perlcabal.org/syn/S05.html&quot; class=&quot;podlinkurl&quot;
&gt;http://perlcabal.org/syn/S05.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;More working (!) examples of regexes and grammars can be found in the &lt;i&gt;November&lt;/i&gt; project, a wiki engine written in Perl 6. See &lt;a href=&quot;http://github.com/viklund/november/&quot; class=&quot;podlinkurl&quot;
&gt;http://github.com/viklund/november/&lt;/a&gt;.&lt;/p&gt;

 </description>
  </item>
  <item rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/19-regex.html">
 <title>Regexes strike back</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/19-regex.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Sun, Nov 30 00:00:00 2008</pubDate>
    <description>&lt;!-- 1227999600 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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;  ~~ mm/ (..) ** 2 / {
        say $1;                 # cd
    }

    # substitute with the :sigspace 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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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;mm/.../&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;) preserve 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                      # FOO&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' href='#___top' title='click to go to top of document'
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 organge banana kiwi&amp;#62;;
    if m:i:s/ (\d+) (@fruits)s? / {
        say &amp;#34;You&amp;#39;ve got $0 $1, 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 rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/18-scoping.html">
 <title>Scoping</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/18-scoping.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Sat, Nov 29 00:00:00 2008</pubDate>
    <description>&lt;!-- 1227913200 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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;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' href='#___top' title='click to go to top of document'
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, then the previous value of the variable is restored. If not, it is kept.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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. Therefor 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' href='#___top' title='click to go to top of document'
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://perlcabal.org/syn/S04.html&quot; class=&quot;podlinkurl&quot;
&gt;http://perlcabal.org/syn/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://perlcabal.org/syn/S02.html#Names&quot; class=&quot;podlinkurl&quot;
&gt;http://perlcabal.org/syn/S02.html#Names&lt;/a&gt;.&lt;/p&gt;

 </description>
  </item>
  <item rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/17-unicode.html">
 <title>Unicode</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/17-unicode.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Fri, Nov 28 00:00:00 2008</pubDate>
    <description>&lt;!-- 1227826800 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://perlcabal.org/syn/S29.html#Str&quot; class=&quot;podlinkurl&quot;
&gt;http://perlcabal.org/syn/S29.html#Str&lt;/a&gt; =for time 1227826800 =for editor vim: spell&lt;/p&gt;

 </description>
  </item>
  <item rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/16-enums.html">
 <title>Enums</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/16-enums.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Thu, Nov 27 00:00:00 2008</pubDate>
    <description>&lt;!-- 1227740400 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;  enum bit 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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://perlcabal.org/syn/S12.html#Enums&quot; class=&quot;podlinkurl&quot;
&gt;http://perlcabal.org/syn/S12.html#Enums&lt;/a&gt; =for time 1227740400 =for editor vim: spell&lt;/p&gt;

 </description>
  </item>
  <item rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/15-twigils.html">
 <title>Twigils</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/15-twigils.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Tue, Oct 21 00:00:00 2008</pubDate>
    <description>&lt;!-- 1224540000 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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.exists(&amp;#39;DOCUMENT_ROOT&amp;#39;);&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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;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;Global variables can be accessed with the &lt;code&gt;*&lt;/code&gt; twigil, so &lt;code&gt;%GLOBAL::ENV&lt;/code&gt; can be abbreviated to &lt;code&gt;%*ENV&lt;/code&gt;, the command line arguments are stored in &lt;code&gt;@*ARGS&lt;/code&gt;, the current process number is in &lt;code&gt;$*PID&lt;/code&gt; and so on.&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' href='#___top' title='click to go to top of document'
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 rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/14-main-sub.html">
 <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>Mon, Oct 20 00:00:00 2008</pubDate>
    <description>&lt;!-- 1224453600 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
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 = glob(&amp;#34;~/&amp;#34;)) {
      # do stuff here
  }

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

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
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=healty           :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' href='#___top' title='click to go to top of document'
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' href='#___top' title='click to go to top of document'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

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

 </description>
  </item>
  <item rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/13-custom-operators.html">
 <title>Custom Operators</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/13-custom-operators.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Sun, Oct 19 00:00:00 2008</pubDate>
    <description>&lt;!-- 1224367200 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 13 - Custom Operators&lt;/p&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    multi sub postfix:&amp;#60;!&amp;#62;(Int $x) {
        my $factorial = 1;
        $factorial *= $_ for 2..$x;
        return $factorial;
    }
    
    say 5!;                     # 120&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Operators are functions with unusual names, and a few additional properties like precedence and associativity. Perl 6 usually follows the pattern &lt;code&gt;term infix term&lt;/code&gt;, where &lt;code&gt;term&lt;/code&gt; can be optionally preceded by prefix operators and followed by postfix or postcircumfix operators.&lt;/p&gt;

&lt;pre&gt;    1 + 1               infix
    +1                  prefix
    $x++                postfix
    &amp;#60;a b c&amp;#62;             circumfix
    @a[1]               postcircumfix&lt;/pre&gt;

&lt;p&gt;Operator names are not limited to &amp;#34;special&amp;#34; characters, they can contain anything except whitespaces.&lt;/p&gt;

&lt;p&gt;The long name of an operator is its type, followed by a colon and a string literal or list of the symbol or symbols, for example &lt;code&gt;infix:&amp;#60;+&amp;#62;&lt;/code&gt; is the the operator in &lt;code&gt;1+2&lt;/code&gt;. Another example is &lt;code&gt;postcircumfix:&amp;#60;[ ]&amp;#62;&lt;/code&gt;, which is the operator in &lt;code&gt;@a[0]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With this knowledge you can already define new operators:&lt;/p&gt;

&lt;pre&gt;    multi sub prefix:&amp;#60;&amp;#8364;&amp;#62; (Str $x) {
        2 *  $x;
    }
    say &amp;#8364;4;                         # 8&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;Precedence&quot;
&gt;Precedence&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;In an expression like &lt;code&gt;$a + $b * $c&lt;/code&gt; the &lt;code&gt;infix:&amp;#60;*&amp;#62;&lt;/code&gt; operator has tighter precedence than &lt;code&gt;infix:&amp;#60;+&amp;#62;&lt;/code&gt;, which is why the expression is evaluated as &lt;code&gt;$a + ($b * $c)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The precedence of a new operator can be specified in comparison to to existing operators:&lt;/p&gt;

&lt;pre&gt;    multi sub infix:&amp;#60;foo&amp;#62; is equiv(&amp;#38;infix:&amp;#60;+&amp;#62;) { ...  }
    mutli sub infix:&amp;#60;bar&amp;#62; is tighter(&amp;#38;infix:&amp;#60;+&amp;#62;) { ... }
    mutli sub infix:&amp;#60;baz&amp;#62; is looser(&amp;#38;infix:&amp;#60;+&amp;#62;) { ... }&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;Associativity&quot;
&gt;Associativity&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Most infix operators take only two arguments. In an expression like &lt;code&gt;1 / 2 / 4&lt;/code&gt; the &lt;i&gt;associativity&lt;/i&gt; of the operator decides the order of evaluation. The &lt;code&gt;infix:&amp;#60;/&amp;#62;&lt;/code&gt; operator is left associative, so this expression is parsed as &lt;code&gt;(1 / 2) / 4&lt;/code&gt;. for a right associative operator like &lt;code&gt;infix:&amp;#60;**&amp;#62;&lt;/code&gt; (exponentiation) &lt;code&gt;2 ** 2 ** 4&lt;/code&gt; is parsed as &lt;code&gt;2 ** (2 ** 4)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Perl 6 has more associativities: &lt;code&gt;none&lt;/code&gt; forbids chaining of operators of the same precedence (for example &lt;code&gt;2 &amp;#60;=&amp;#62; 3 &amp;#60;=&amp;#62; 4&lt;/code&gt; is forbidden), and &lt;code&gt;infix:&amp;#60;,&amp;#62;&lt;/code&gt; has &lt;code&gt;list&lt;/code&gt; associativity. &lt;code&gt;1, 2, 3&lt;/code&gt; is translated to &lt;code&gt;infix:&amp;#60;,&amp;#62;(1; 2; 3)&lt;/code&gt;. Finally there&amp;#39;s the &lt;code&gt;chain&lt;/code&gt; associativity: &lt;code&gt;$a &amp;#60; $b &amp;#60; $c&lt;/code&gt; translates to &lt;code&gt;($a &amp;#60; $b) &amp;#38;&amp;#38; ($b &amp;#60; $c)&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    multi sub infix:&amp;#60;foo&amp;#62; is tighter(&amp;#38;infix:&amp;#60;+&amp;#62;)
                          is assoc(&amp;#39;left&amp;#39;)
                          ($a, $b) {
        ...
    }&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;Postfcircumfix_and_Circumfix&quot;
&gt;Postfcircumfix and Circumfix&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Postfcircumfix operators are method calls:&lt;/p&gt;

&lt;pre&gt;    class OrderedHash is Hash {
        method postcircumfix:&amp;#60;{ }&amp;#62;(Str $key) {
            ...
        }
    }&lt;/pre&gt;

&lt;p&gt;If you call that as &lt;code&gt;$object[$stuff]&lt;/code&gt;, &lt;code&gt;$stuff&lt;/code&gt; will be passed as an argument to the method, and &lt;code&gt;$object&lt;/code&gt; is available as &lt;code&gt;self&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Circumfix operators usually imply a different syntax (like in &lt;code&gt;my @list = &amp;#60;a b c&amp;#62;;&lt;/code&gt;), and are thus implemented as macros:&lt;/p&gt;

&lt;pre&gt;    macro circumfix:&amp;#171;&amp;#60; &amp;#62;&amp;#187;($text) is parsed / &amp;#60;-[&amp;#62;]&amp;#62;+ / {
        return $text.comb(rx/\S+/);
    }&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;is parsed&lt;/code&gt; trait is followed by a regex that parses everything between the delimiters. If no such rule is given, it is parsed as normal Perl 6 code (which is usually not what you want if you introduce a new syntax). &lt;code&gt;Str.comb&lt;/code&gt; searches for occurrences of a regex and returns a list of the text of all matches.&lt;/p&gt;

&lt;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;&amp;#34;Overload&amp;#34;_existing_operators&quot;
&gt;&amp;#34;Overload&amp;#34; existing operators&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Most (if not all) existing operators are multi subs or methods, and can therefore be customized for new types. Adding a multi sub is the way of &amp;#34;overloading&amp;#34; operators.&lt;/p&gt;

&lt;pre&gt;    class MyStr { ... }
    multi sub infix:&amp;#60;~&amp;#62;(MyStr $this, Str $other) { ... }&lt;/pre&gt;

&lt;p&gt;This means that you can write objects that behave just like the built in &amp;#34;special&amp;#34; objects like &lt;code&gt;Str&lt;/code&gt;, &lt;code&gt;Int&lt;/code&gt; etc.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Allowing the user to declare new operators and &amp;#34;overload&amp;#34; existing ones makes user defined types just as powerful and useful as built in types. If the built in ones turn out to be insufficient, you can replace them with new ones that better fit your situation, without changing anything in the compiler.&lt;/p&gt;

&lt;p&gt;It also removes the gap between using a language and modifying the language.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://perlcabal.org/syn/S06.html#Operator_overloading&quot; class=&quot;podlinkurl&quot;
&gt;http://perlcabal.org/syn/S06.html#Operator_overloading&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are interested in the technical background, ie how Perl 6 can implement such operator changes and other grammar changes, read &lt;a href=&quot;http://perlgeek.de/en/article/mutable-grammar-for-perl-6&quot; class=&quot;podlinkurl&quot;
&gt;http://perlgeek.de/en/article/mutable-grammar-for-perl-6&lt;/a&gt;.&lt;/p&gt;

 </description>
  </item>
  <item rdf:about="http://perlgeek.de/blog-en/perl-5-to-6/12-lazyness.html">
 <title>Laziness</title>
  <link>http://perlgeek.de/blog-en/perl-5-to-6/12-lazyness.html</link>
 <author>Moritz Lenz</author>
   <pubDate>Sat, Oct 18 00:00:00 2008</pubDate>
    <description>&lt;!-- 1224280800 --&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;NAME&quot;
&gt;NAME&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&amp;#34;Perl 5 to 6&amp;#34; Lesson 12 - Laziness&lt;/p&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;SYNOPSIS&quot;
&gt;SYNOPSIS&lt;/a&gt;&lt;/h3&gt;

&lt;pre&gt;    my @integers = 0..*;
    for @integers -&amp;#62; $i {
        say $i;
        last if $i % 17 == 0;
    }

    my @even = map { 2 * $_ }, 0..*;
    my @stuff = gather {
        for 0 .. Inf {
            take 2 ** $_;
        }
    }&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;DESCRIPTION&quot;
&gt;DESCRIPTION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Perl programmers tend to be lazy. And so are their lists.&lt;/p&gt;

&lt;p&gt;In this case &lt;i&gt;lazy&lt;/i&gt; means, that the evaluation is delayed as much as possible. When you write something like &lt;code&gt;@a = map BLOCK, @b&lt;/code&gt;, the block isn&amp;#39;t executed at all. Only when you start to access items from &lt;code&gt;@a&lt;/code&gt; the &lt;code&gt;map&lt;/code&gt; actually executes the block and fills &lt;code&gt;@a&lt;/code&gt; as much as needed.&lt;/p&gt;

&lt;p&gt;Laziness allows you to deal with infinite lists: as long as you don&amp;#39;t do anything to all of its arguments, they take up only as much space as the items need that have already been evaluated.&lt;/p&gt;

&lt;p&gt;There are pitfalls, though: determining the length of a list or sorting it kills laziness - if the list is infinite, it will likely loop infinitely.&lt;/p&gt;

&lt;p&gt;In general all conversions to a scalar (like &lt;code&gt;List.join&lt;/code&gt;) are &lt;i&gt;eager&lt;/i&gt;, i.e. non-lazy.&lt;/p&gt;

&lt;p&gt;Laziness prevents unnecessary computations, and can therefor boost performance while keeping code simple.&lt;/p&gt;

&lt;p&gt;When you read a file line by line in Perl 5, you don&amp;#39;t use &lt;code&gt;for (&amp;#60;HANDLE&amp;#62;)&lt;/code&gt; because it reads all the file into memory, and only then starts iterating. With laziness that&amp;#39;s not an issue:&lt;/p&gt;

&lt;pre&gt;    my $file = open &amp;#39;/etc/passwd&amp;#39;, :r;
    for $file.lines -&amp;#62; $line {
        say $line;
    }&lt;/pre&gt;

&lt;p&gt;Since &lt;code&gt;$file.lines&lt;/code&gt; is an iterator or a lazy list (it doesn&amp;#39;t really matter which), the lines are only physically read from disk as needed (besides buffering, of course).&lt;/p&gt;

&lt;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;gather/take&quot;
&gt;gather/take&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;A very useful construct for creating lazy lists is &lt;code&gt;gather { take }&lt;/code&gt;. It is used like this:&lt;/p&gt;

&lt;pre&gt;    my @list = gather {
        while 1 {
            # some computations;
            take $result;
        }
    }&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;gather BLOCK&lt;/code&gt; returns a lazy list. When items from &lt;code&gt;@list&lt;/code&gt; are needed, the &lt;code&gt;BLOCK&lt;/code&gt; is run until &lt;code&gt;take&lt;/code&gt; is executed. &lt;code&gt;take&lt;/code&gt; is just like return, and all &lt;code&gt;take&lt;/code&gt;n items are used to construct &lt;code&gt;@list&lt;/code&gt;. When more items from &lt;code&gt;@list&lt;/code&gt; are needed, the execution of the block is resumed after &lt;code&gt;take&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gather/take&lt;/code&gt; is dynamically scoped, so this is allowed:&lt;/p&gt;

&lt;pre&gt;    my @list = gather {
        for 1..10 {
            do_some_computation($_);
        }
    }

    sub do_some_computation($x) {
        take $x * ($x + 1);
    }&lt;/pre&gt;

&lt;h4&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;Controlling_Laziness&quot;
&gt;Controlling Laziness&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Laziness has its problems (and when you try to learn Haskell you&amp;#39;ll notice how weird their IO system is because Haskell is both lazy and free of side effects), and sometimes you don&amp;#39;t want stuff to be lazy. In this case you can just prefix it with &lt;code&gt;eager&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    my @list = eager map { $block_with_side_effects }, @list;&lt;/pre&gt;

&lt;p&gt;On the other hand only lists are lazy by default. But you can also make lazy scalars:&lt;/p&gt;

&lt;pre&gt;    my $ls = lazy { $expansive_computation };&lt;/pre&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;MOTIVATION&quot;
&gt;MOTIVATION&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;In computer science most problems can be described with a tree of possible combinations, in which a solution is being searched for. The key to efficient algorithms is not only to find an efficient way to search, but also to construct only the interesting parts of the tree.&lt;/p&gt;

&lt;p&gt;With lazy lists you can recursively define this tree and search in it, and it automatically constructs only these parts of the tree that you&amp;#39;re actually using.&lt;/p&gt;

&lt;p&gt;In general laziness makes programming easier because you don&amp;#39;t have to know if the result of a computation will be used at all - you just make it lazy, and if it&amp;#39;s not used the computation isn&amp;#39;t executed at all. If it&amp;#39;s used, you lost nothing.&lt;/p&gt;

&lt;h3&gt;&lt;a class='u' href='#___top' title='click to go to top of document'
name=&quot;SEE_ALSO&quot;
&gt;SEE ALSO&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://perlcabal.org/syn/S02.html#Lists&quot; class=&quot;podlinkurl&quot;
&gt;http://perlcabal.org/syn/S02.html#Lists&lt;/a&gt;&lt;/p&gt;

 </description>
  </item>
 
</rdf:RDF>