Day 141: Hackathon day #2: More revelations

autrijus on 2005-06-25T02:17:51

(to be continued...)
(and no, Perl 6 was too much fun so that we didn't tackle the ICFP contest in the end. sadly.)

  • Roles are also classes! They can be instantiated just fine if they are concrete enough. Basically they mean composable classes or mixin-able classes. Hence, RoleName.new() instantiates an object that will probably fail on all stubs.
  • Class-defined methods with the same short name as their roles must conform to the same signature as the role methods; otherwise a fatal exception occurs.
  • Abstract pure methods in Roles really just return an Exception object, exactly the same way as if the ... is evaluated normally.
    method foo () { ... } 
    
  • Perl 6 is defined with separate compilation in mind; each compilation unit is going to pretend they are compiled in a different process. They are allowed to bind names (exports) in other namespaces, but they cannot read anything from others.
  • &chomp and &wrap are now nondestructive; chomp returns the chomped part, which can be defined by the filehandle that obtains the default string at the first place. To get destructive behaviour, use the .= form.
  • Filehandles opens chomped by default; you need to add the :newlines flag to turn off chumping.
    my $fh = open 'file';                   # autochomp
    my $fh = open 'file', :newlines;        # non-autochomp
    $fh.newline = "\r";                     # now split on \r
    $str = $fh.readline;
    $str.newline;                           # \r
    
    for chomp =$fh { ... }
    

    If .newline is set to a rule, then its captured variables are made available to the calling block as if it has done a matching.

  • The chained associativity level is associative with the precendence level it is in; hence, in the same level all chainfix operators glue together the same way the chained comparisons glue together. The listfix associativity always accept one trailing operator, so 1 Y 2 Y 3 Y is legal.
  • Rules have their grammatical category, &rule:<foo>, that can reside in either grammars, non-grammar packages, or the true global namespace where they get names like &*rule:<ident>. Inside grammars they are called &Foo::rule:<blah>.
  • Grammar is a specialized form of Role (which is a special form of Class, which is a special form of Package). When a grammar does another grammar, it mixes in the grammar's rules as package globals.
  • To invoke an unqualified subrule, look at the lexical scope of the invocation site, then package globals, then finally global builtins. This is exactly the same order as a normal subroutine lookup. One can think rules as submethods that gets mixed in with a does call. You call them with the current invocant (i.e. the Grammar):
    # calling 
    &rule:.($?SELF: $state);
    
  • To invoke a qualified subrule, one look the package-level named rule up, then invoke it with two parameters: The invocant is the package object that subrule belongs to; the argument is the state object the rule engine is currently treading upon:
    # calling 
    &Grammar::rule:($Grammar: $state);