Quick jump:  What's new

 
Go to:
 
Weblog: Tablet 
in Perl 6
Perl 6 Language Design Tablet

Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary




Larry Wall: You should not take this picture to mean that linguistics is the opposite of common sense. But there are many computer scientists who have complained that it should be more like engineering and less like an art. And I disagree, but that's okay.

Main Goal

Enjoy life and enjoy the freedom of expressing yourself. Which means: support you as much as possible, and not force you in any way. Perl 6 tries to be even better than Perl 5 as your humble servant for translating your thoughts into executable code (if possible).

Strategy

Every style and level of expertise has to be supported. And even more difficult: all of them have to play together well. The default often imitates a natural language (e.g., English). The following oft-repeated terms encapsulate strategies and concepts for this.

Terms

TIMTOWTDI

Perl's everlasting slogan: "There is more than one way to do it," meaning your way is valid, too. Just be aware of logical consequences.

Visual Metaphor

People like to see their code as a picture at first glance. Therefore names and operators should be distinctive, similar things should look similar, different things different. Larry likes to add: weird things should also look weird. And sometimes even characters can be ideograms; like the Z meta-operator, which shows visually how it works (first left then right, then next left, etc.).

Topicalizer

This also has to do with natural attention flow. If a topic or first word of a sentence sets an understandable context for what follows, the meaning is built up word for word. This is much easier to grasp, than if the last word of a block resolves the block's entire meaning (like regex modifiers do in Perl 5). In particular, for and given are called topicalizers, because they set $_, the current topic variable.

Context sensitive

The same term or symbol might do different things in different contexts, although we have done our very best to consistently represent the same general idea. Maybe the best example is the * aka Whatever. But every context is its own domain, optimized to solve the problems of that domain.

Maximum reuse

Not only is syntax reused, to minimize the stuff you have to remember, but concepts are reused also, wherever possible. For example, Pairs are used to build hashes or named parameters in routines calls. There are plenty more examples. Maybe the biggest one: the regex engine that parses your code is the same engine you use in your program.

No special rules

Most of the special rules that plagued Perl 5 are gone or generalized.

One pass parsing

Perl 6 is much easier to understand, both for people and also for software parsers. The language is designed to be understood by machines in one read, dividing the source in smaller and smaller units. As a nice side effect: syntax errors can only damage the understanding of small units.

Keep easy things easy and hard things possible

Beside TIMTOWTDI, the other ancient slogan is "Make easy things easy and hard things possible." Even if nearly every cool idea is crammed into Perl 6, we don't lose track of the basics. Features are made as accessible and free of artificial prerequisites as possible. For example, printing a variable as a line without explicit quotes and newline (say $var), or reading a file without a file handle (slurp("filename").lines). Rule of thumb: if you can think of a concept as trivial, then it should be. On the other hand, if you can think of a computable solution, it should at least be doable in Perl (no artificial walls).

Huffman coding

This is a refinement of the last rule: common, often used things have short, straightforward names. More verbose names indicate more rare, harder to grasp concepts, or things that overwrite the default rules ("weird things should look weird"). It also implies that the defaults lean toward what a beginner (non-Perl 5 programmer) might expect ("hide the fancy stuff").

Water bed theory of linguistic complexity

This philosophy underlies the two previous terms. All Perl words should reflect logical units which can be freely combined. Making it more complicated than necessary is obviously not desirable. But oversimplification will create artificial complexity somewhere else. Like a water bed, where you can press down in one place but the water will go up in another.

All your paradigms are belong to us

We learn, take and adapt from every source possible to offer the best of all breeds. You should be able to keep your style when switching to Perl. Perl 6 supports the structured, object oriented, aspect oriented, functional, design by contract, declarative and logic programming paradigms.

Buzzword compliant

This term is an extension to the previous. Not only every programming paradigm, but also every technique or neat trick loudly marketed out there as "must have" "in the [%DATE%]", should be available in Perl 6 (or at least be possible), as long there is expressive value in it. A few examples include "named parameters", "lvalue subroutines", "parametizable subclasses" or "hygienic macros".

Second system syndrome

We are very aware that many ambitious software projects that are rebuilt from scratch fail. Especially those which aim to be "even better in all ways" successors of already successful creations. In fact, this happens so often that the term "second system syndrome" was coined. We chose this path for Perl 6 anyway, because we believe it's the only way to get a significantly better language. (Gradual improvements are flowing into Perl 5 all the time, sometimes inspired from Perl 6.) That's why the unofficial motto of Perl 6 is "Second system syndrome done right".

As a consequence of these principles, various trends can be observed.

Design Process

The initial source of the Perl 6 design came from 361 RFC (request for comments) submissions by people from all over the world. Larry Wall sorted and commented on them in several documents called the Apocalypses (meaning "revelations"). Damian Conway wrote, for each Apocalypse, an Exegesis ("explanation"), that expounded Larry's ideas. These are now all historic documents that not have changed since, because they were much harder to maintain than the Synopses ("abstracts" or "comparisons" (with Perl 5)). The Synopsis documents, along with the test suite, form the official specification of Perl 6. More details on that can be found in the history section.

The Synopses are continually edited because the more we think about the syntax and semantics, the more detailed the picture gets. The other important input comes from the implementors. They uncover cases that worked only in theory, and important corner cases not thought about before. This mutual influence of specification and implementation we call the whirlpool model of software development, which is an intended pun on the well-known waterfall model.

Design changes are usually discussed in the p6l mailing list and the #perl6 channel on the freenode IRC network.


Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary


permalink
Perl 6 Tablets

These are the Perl 6 Tablets, a comprehensive manual, aimed to support many different ways of learning. The content is nicely sorted and indexed and many links allow you to follow your interest freely. For a lightweight introduction, try the Perl 6 Tutorial. If its too easy, read the specs.


DISCLAIMER: This docs moved to tablets.perl6.org/ and no longer represent the current state.
Maybe this will vanish soon!


Preface and Introduction

Tablet 0: History

Tablet 1: Language Design

Tablet 2: Basic Syntax (Spaces, Comments, Literals, Quoting, Formatting)

Tablet 3: Variables (Sigils, Twigils, Assignment, Typing, Scopes)

Tablet 4: Operators (Comparison, Math, String, Logic, Metaops)

Tablet 5: IO (Command Line, Files, Sockets, Network, Misc.)

Tablet 6: Blocks (Conditions, Loops)

Tablet 7: Subroutines (Signatures, Modules)

Tablet 8: Objects (Classes, Roles)

Tablet 9: Regex (Rules, Grammars)

Tablet 10: Metaprogramming (Macros, DSL)


Appendix A: Index (all ops, builtins, methods and it terms, alphabetically ordered)

Appendix B: Tables, short reference (cheat sheets ans summary tables)

Appendix C: Cookbook (chunks of everyday Perl 5, translated into idiomatic Perl 6)

Appendix D: Perl 5-6 Delta

Appendix E: Exellence, Appetizer, best of tour

Appendix F: Links

Appendix G: Glossary

permalink
Perl 6 Meta Tablet

Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary




Macros

Clean Macros

Dirty Macros

Operators

our &postfix:<²> := &infix:<**>.assuming(b => 2);

Changing Internal Grammar

DSL


Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary


permalink
Perl 6 Links Tablet

Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary




Project Pages

Downloads (sources, binaries):

CPAN:

  • Perl6::* modules backports of many Perl 6 features in Perl 5
  • Bundle::Perl6 Pakage of all Perl 6 related Module (except Moose :))
  • p6doc aka Perl6::Doc: a small command line viewer and many Perl 6 documents

Docs

Wikis:

more Info:

News:

IRC:


Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary


permalink
Perl 6 Regex Tablet

Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary




Larry Wall: We put the "Regular" back into "Expression".

Like quotes, are Regular Expressions now a sublanguage, a language inside the language, instead of being just funny strings. They are parsed by a completely different set of rules, which are stored in the special variable $~Regex ($~Trans for transliterations and $~P5Regex for compatible Regexes). But these rules are made much more consistant with the main language than before.

Literal Character

Every alphanumeric character has no special meaning and stand for the character visible. If a backslash (\) is standing directly in front of the character it becomes a metacharacter. For not alphanumeric chars the opposite is true. Inside of quotes every char is literal.

Metacharacter

table

Modifier

Operators

rx/.../ aka Q :regex /.../
m/.../ aka Q :match /.../
ms/.../ aka Q :match :sigspace /.../.../
s/.../.../ aka Q :subst /.../.../
ss/.../.../ aka Q :subst :samespace /.../.../
tr/.../.../ aka Q :trans /.../.../

Methods

comb
match
subst
trans

Special Variables

$/
$<..>
$()
$0 ..

Matching

Substitution

Transliteration

my $escaped = $html.trans(
[ '&', '<', '>' ] =>
[ '&amp;', '&lt;', '&gt;' ]
);

Grammars

Predefined Rules

Are listed here. <prior> is somehow an exception.

all slang variables


Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary


permalink
Perl 6 OOP Tablet

Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendices: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary




Damian Conway: In order to understand how object orientation works in Perl, we first need to agree what object orientation actually is. And that's surprisingly hard to do. Object-oriented programming has been around for at least three decades now, and in that time many opinions, theories, and even ideologies have been formulated on the subject. Each has purported to definitively characterize object orientation. Most are mutually inconsistent. No two are exactly alike.

Classes

Attributes

Methods

Submethods

Inheritance

Delegation

Roles


Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendices: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary


permalink
Best Of Perl 6 Tablet

Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary




This is a small tour, showing the best features and improvements of Perl 6 in short examples, compared to similar working Perl 5 examples.

Command Line

Making output will become simpler and easier for newbies.

               Perl 5                                     Perl 6 
 print "bananas are good\n";                     say "bananas are good";
 print "and I said: $quotes{\"me\"}\n";          say "and I said: %quotes{"me"}.";
 print "and I said: $quotes{\"me\"}\n";          say "and I said: %quotes<me>.";
 print "What is ... ";                           $result = prompt "What is ... ";
 chomp($result = <>);

File IO

File::Slurp is in the Perl 6 core language and file reading is simpler then ever (and autochomp included).

               Perl 5                                     Perl 6 
 $content = do { local $/;                       $content = slurp "poetry.txt";
    open my $FH, "poetry.txt"; <$FH>
 };

chomp(@content = do {                            @content = lines "poetry.txt";
    open my $FH, "poetry.txt"; <$FH>
});

Automatic multithreading

Applying operations to junctions and arrays is now syntactically compact and readable. Perl 6 will create threads where appropriate to use multiple processors, cores or hyperthreading for high level language SIMD concurrent processing.

               Perl 5                                     Perl 6 
 my $sum;                                        my $sum = [+] @numbers;
 $sum += $_ for @numbers;
 for (0 .. $#factor1) {                          @product = @factor1 >>*<< @factor2;
   $product[$] = $factor1[$] * $factor2[$_];
 }

The Perl 5 code is a simplification, of course Perl6 "does the right thing" when the arrays have different lengths.

Comparison

Here are junctions, then chained comparison operators.

               Perl 5                                     Perl 6 
 if ($a == 3 or $a == 4 or $a == 7) {...}        if $a = 3 | 4 | 7 {...}
 if (4 < $a and $a < 12) {...}                   if 4 < $a < 12 {...} 
 if (4 < $a and $a <= 12) {...}                  if $a ~~ 4^..12 {...}
 $a = defined $b ? $b : $c;                      $a = $b // $c;

The defined-OR operator eases lot of cases where Perl 5 newbies could fall into traps.

Case construct

               Perl 5                                      Perl 6 
                                                     given $a {
 if ($a == 2 or $a == 5) {...} }}                      when 2 | 5 {...}
 elsif ($a == 6 or $a == 7 or $a == 8 or $a == 9) {}   when 6 .. 9 {...} 
 elsif ($a =~ /g/) {...}                               when 'g' {...}
 else {...}                                            default {...}
                                                     }

That new construct (backported to 5.10) is clear to read, very versatile and when used in combination with junctions, becomes even clearer.

Powerful for loops

List iteration via for is now much more versatile.

               Perl 5                                     Perl 6 
 for my $i (0..15) {...}                         for ^16 -> $i {...}
 for (my $i=15; $i>1; $i-2) {...}                for 15,*-2...1 -> $i {...}
 for my $key (keys %hash) {                      for %hash.kv -> $key, $value {
   print "$key => $hash{$key}\n"; ...              say "$key => $value"; ...
 for my $i (0..$#a) {                            for zip(@a; @b; @c) -> $a, $b, $c {...}
   my $a = @a[$i];
   my $b = @b[$i];
   my $c = @c[$i]; ... 

Named parameters in subroutines

               Perl 5                                     Perl 6 
 sub routine {                                   sub routine ($a, $b, *@rest) {...}
   my $a = shift;
   my $b = shift;
   my @rest = @_;
 }

Objects with auto generated new and getters and setters

Simple Object creation is now as easy as it gets.

               Perl 5                                     Perl 6 
 package Heart::Gold;                            class Heart::Gold {
                                                   has $.speed;
 sub new {                                         method stop { $.speed = 0 }      
   bless {speed => 0 }, shift;                   }  
 }                                               
                                                 my Heart::Gold $hg1 .= new;
 sub speed {                                     $hg1.speed = 100;
   my $self = shift;                             my $hg2 = $hg1.clone;
   my $speed = shift;
   if (defined $speed) { $self->{speed} = $speed }
   else { $self->{speed} }
 }

 sub stop {
   my $self = shift; 
   $self->{speed} = 0;
 }

The new, getter, setter and clone methods are auto generated. Methods now are called "method".


Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary


permalink
Perl 6 Subroutine Tablet

Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary




Basics

sub

yadda

anonymous

pointy

return

Signatures

Positional Parameter

Named Parameter

Optional Parameter

Required Parameter

Multiple Dispatch

Currying

Wrapping

Namespaces

Scope

Package

Module

Class


Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary


permalink
Perl 6 Delta Tablet

Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary




Ubiquity

As always, Perl is optimized for freedom and richness of expression.

General Purpose

Perl 5 still has a lot of builtins, showing for which task the language was created. But to become more general purpose, the core was enhanced and some other parts got removed. File IO, Network and IPC, Formats, and finally that long deprecated pre P5 DB-binding (dbmopen) went out of the core or at least into sensible module namespace. The optional type system enables compiler optimisations to make Perl even usable in cases where you had to switch to C before.

Less Unix Centric

Perl 1 was a child of the Unix world, borrowing a lot of folklore and words. As Perl 6 had to regularize the Regex and reform some other parts, this heritage has become less visible.

Unicode

Per default all Perl 6 code is Unicode and you can use any weird character for any part of the syntax.

Simple English

In Perl 5 we had my, our, use, require, bless, can, ISA (is a), chop and also of course if, else, goto and all the loops. Perl 6 adds given, when, gather, take, loop, WHERE, HOW, WHO and lot more everyday English, which should make it easy to read and understand.

Hidden Complexity

Even simple Perl 6 looks pretty much like older Perl, it has a tremendous complexity inside, which only have to show up if you really need it.

Functional Programming

High order functions, currying, you name it. All that goodies are there, as Perl 6 aims to a full support of functional programming.

Object Oriented

OOP syntax is now more simplified, compact, knows about encapsulation and has many new features, including dynamic class composition.

Like in Smalltalk or Ruby every "thing" (values, vars, blocks, routines) is an object and ops are "just" methods. It helps to have internally everything in its place. But programmer still can pretend it's not the case.

Full Introspection

Because every object knows his attributes and methods, you can ask the compiler nearly anything about the state of your program.

Metalanguage

And because classes are made to get extended or changed, Perl 6 has not only full metaprogramming capabilities, beyond your wildest dreams. It is a metalanguage with a beginner friendly default that aims to become a family of languages. Rule of thumb: everything is allowed, as long as you declare it.

One Spec - Many Implementations

Unlike every other Perl so far, Perl 6 has a precise specification, written in Perl 6. This allows to have several implementations, even syntactical compatible ones.

Syntactical Changes

  • use strict and warnings and utf are on by default

Operator

  • -> (arrow) becomes . (dot) - for method calls: object.method;
  • =~ becomes ~~ - use regex now only with smartmatch
  • qw( ... )(exists still) becomes < ... > - with interpolation and quote protection: << ... >>
  • | becomes +| - logical bit operation in numerical context
  • & becomes +&
  • ^ becomes +^
  • << becomes +< - shifting numerical values
  • >> becomes +>
  • . becomes ~ (tilde) - for concatenate strings: 'Shinmen Musahashi: ' ~ 'Sho Rin No Sho';
  • .. becomes ff - flipflop operator
  • ... becomes fff
  • cmp becomes leg - cmp is now for general comparison, leg in string context
  • ? : becomes ?? !! - ternary operator
  • length becomes chars - number of chars in a string

Variable

  • consistent sigils: so @array[1] replaces $array[1] and %hash{'key'} replaces $hash{'key'} and %hash{'key1', 'key2'} replaces @hash{'key1', 'key2'}
  • scalar @a becomes +@a
  • arrays, hashes, formats, directories and filehandles are internal objects with procedural Interfaces and can be saved in scalar variables.
  • special variables have their own namespace ($*VARNAME and $?VARNAME), also token (__LINE__) will now be found there

Flow Control

  • no more 'foreach'. 'for' now iterates only on arrays
  • for ($a = 0; $a++; $a < 5) becomes loop ($a = 0; $a++; $a < 5)
  • do {...} while (...); becomes repeat {...} while ...;
  • define classes with 'class' and modules with 'module'
  • no more source filters, but macros
  • no more references, bind variables on the symbol table

Traps

  • no more each, use .pairs or .kv instead
  • no more C-style for, use loop instead
  • length( @a) is now elems @a and length $str is chars $str;

News

  • new contexts: hashlist, lazy list, bool, int, num, string
  • optional typing, traits (properties of variables), binding
  • loop and say commands
  • zip, err, yadda and pipe operators
  • hyper / reduction operators
  • chained comparison operators
  • case construct with given and when
  • junctions : if $a = 1|2|3
  • currying
  • smartmatch / rules (regex++, own sublanguage within the language like in Rebol)
  • named parameters for subroutines, multimethods, coroutines
  • more explicit OOP:
    • autogenerated getters/setters
    • roles (similar to Ruby's Mixins, or Java's Interfaces)
  • delegation, design by contract (Eiffel)
  • exceptions, continuations
  • macros

Native Perl 6 (new idioms)

  • no parentheses required for control expressions after if, unless, while ... (but still allowed)
  • %hash<key> instead of %hash{'key'}, or %hash<< key1 key2 >> instead of %hash{ 'key1', 'key2' }
  • loop instead while (1)

For more native Perl 6, read the cookbook.


Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary


permalink
Perl 6 Block Tablet

Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary




Block

do

gather and take

goto

leave

sub

Conditional

if

binary contect

elsif

else

unless

given

when

continue

default

Loops

loop

void context

while

binary contect

until

binary contect

for

lazy list contect

repeat


Intro - Chapter: 0:History 1:Design 2:Basics 3:Var 4:Op 5:IO 6:Block 7:Sub 8:OOP 9:Rx 10:Meta
Overview - Appendix: A:Index B:Tables C:Cook D:Delta E:Best of F:Links G:Glossary


permalink
Weblog Navigation
Loading...
Weblog Archives
  • Loading...

Upload Files

Click "Browse" to find the file you want to upload. When you click "Upload file" your file will be uploaded and added to the list of attachments for this page.

Maximum file size: 50MB

 
 
 
File Name Author Date Uploaded Size

Save Page As

Enter a meaningful and distinctive title for your page.

Page Title:

Tip: You'll be able to find this page later by using the title you choose.

Page Already Exists

There is already a page named XXX. Would you like to:

Save with a different name:

Save the page with the name "XXX"

Append your text to the bottom of the existing page named: "XXX"

Upload Files

Click "Browse" to find the file you want to upload. When you click "Add file" this file will be added to the list of attachments for this page, and uploaded when you save the page.

 
 
 
Add Tags

Enter a tag and click "Add tag". The tag will be saved when you save the page.

Tag: 

Suggestions: