Perl 6 |
Perl 6 is the next version of the Perl programming language, currently under development. The vision for Perl 6 is more than simply a rewrite of Perl 5.
Perl 6 is not intended to be backwards-compatible, though there will be a compatibility mode. Larry Wall, the creator of Perl, has called Perl 6 the community s rewrite of Perl , because he has based the changes largely on 361 requests for comments submitted by the Perl community in 2000. He is outlining these changes in a series of long essays, called Apocalypses, which are numbered to correspond to chapters in Programming Perl ( The Camel Book ). The current, unfinalized, specification of Perl 6 is encapsulated in design documents called Synopses, which are numbered to correspond to Apocalypses.
Pugs is an implementation of Perl 6 in the Haskell programming language that will be used for bootstrapping. Pugs s goal is to write the Perl 6 compiler in Perl 6 itself, possibly by translating its source code to Perl 6. After that, Perl 6 will be self-hosted—it will be used to compile itself. Much of the implementation of Perl will then be exposed, making it possible to, for example, extend the parser.
Parrot virtual machine is a Virtual machine designed for interpreted languages, primarily for Perl 6. Pugs can execute the code directly, as well as compile Perl 6 to JavaScript, Perl 5 or Parrot bytecode. The self-hosting Perl 6 compiler will target (and also run on) Parrot.
= Major changes from Perl 5 =
Perl 5 and Perl 6 differ fundamentally, though in general the intent has been to keep Perl 6 Perl . Most of the changes are intended to normalize the language, to make it easier for learning and expert programmers alike to understand, and to make easy things easier and hard things more possible .
== A Specification ==
The most important single difference between Perl 5 and Perl 6 is that Perl 6 began life as a specification. This means that Perl 6 can be re-implemented if needed, and it also means that you don t have to read the source code for the ultimate authority on any given feature. While Perl 5 s documentation was excellent, if the documentation and the source code of the Perl 5 interpreter disagreed, the documentation would be changed.
== A Type System ==
In Perl 6, the Datatype#Static_and_dynamic_typing of Perl 5 has been augmented by the addition of static types. For example: my int $i = 0; my num $n = 3.141; my str $s = Hello, world ; However, as with Perl 5, programmers can do most things without any explicit typing at all: my $i = 25 + 10;
Static typing is beneficial for reducing subtle errors and increasing maintainability, especially in large software projects. But static typing is a burden when writing quick scripting programming languages, one-liners or one-off code (i.e., code that is written to achieve some temporary purpose, run once and retired), purposes that have long been a mainstay of Perl. A currently unresolved debate in the Perl 6 design community regards how code written without types interacts with code using types.
== Formal Subroutine Parameter Lists ==
Perl 5 defined subroutines without parameter (computer science) lists at all (though simple parameter counting and some very loose type checking can be done using Perl 5 s prototypes ). Subroutine arguments passed in became aliases into elements of the array @_. If @_ were modified, the changes would be reflected in the original data: # Perl 5 code sub incr { $_[0]++ } my $x = 1; incr($x); # $x is now 2 incr(3); # runtime error: Modification of a read-only value attempted
Perl 6 introduces true formal parameters to the language. In Perl 6, a subroutine declaration looks something like this: sub do_something(Str $thing, Int $other) { ... }
As in Perl 5, the formal parameters (i.e., the pseudo-variables in the parameter list) are aliases to the actual parameters (the values passed in), but by default, the aliases are marked Variable#Constant so they cannot be modified: sub incr(Num $x) { $x++; # compile-time error } If a formal parameter is followed by is copy or is rw, however, it can be modified. In the is copy case, Perl 6 copies the actual parameter s data rather than aliasing it; so it can be modified, but changes are local to the subroutine. In the is rw case (rw stands for read-write ), the alias is not marked constant. This change also catches at compile-time errors such as the one above: sub incr(Num $x is rw) { $x++ } incr(3); #compile-time error
There are a number of features in parameter lists which make parameter passing much more powerful than in Perl 5:
For example: sub my_split(Rule $pat = rx/s+/, Str $expr = $_, Int $lim where $^lim >= 0) { ... }
== Sigil invariance ==
In Perl 5, Sigil (computer programming)—the punctuation characters that precede a variable name—changed depending on how the variable was used: # Perl 5 code my @array = (0, 1, 2, 3); my $element = $array[1]; # $element equals 1 In Perl 6, sigils are invariant: my @array = (0, 1, 2, 3); my $element = @array[1]; # $element equals 1 This change is meant to reduce the cognitive load of recognizing that a variable spelled $array... is actually the variable @array.
== Object orientation ==
Perl 5 supported object orientation via a mechanism known as blessing . Any reference (computer science) could be blessed into being an object of a particular class, as so: # Perl 5 code my $object = bless $reference, Class ; A blessed object could then have method (computer science)s invoked on it using the arrow syntax : # Perl 5 code $object->method(); which would cause Perl to locate ( dispatch ) an appropriate Subroutine named method, and call it with $object as its first argument.
While extremely powerful—virtually any other computer language s object model could be simulated using this simple facility—it made the commonest case of object orientation, a struct-like object with some associated code, unnecessarily difficult. In addition, because Perl could make no assumptions about the object model in use, method invocation could not be optimized very well.
In the spirit of making the easy things easy but hard things possible , Perl 6 retains the blessing model for programmers who desire unusual behavior, but supplies a more robust object model for the common cases. For example, a class to encapsulate a Cartesian coordinate system point (geometry) could be written:
class Point is rw { has $.x; has $.y; } and then used: my Point $point .= new; $point.x = 1.2; $point.y = -3.7;
The dot replaces the arrow in a nod to the many other languages, e.g. Java programming language, Python programming language, and Ruby programming language, that have coalesced around dot as the syntax for method invocation.
== Rules: the new regexes ==
Perl s regular expression and string-processing support has always been one of its defining features. Unlike most other languages, in which regular expressions are provided by a library (computer science), Perl has pattern matching facilities built-in to the language.
Though called regular expressions , or simply regexes , Perl s pattern-matching construct has exceeded the capabilities of formal regular expressions for some time. For that reason, the regexes in Perl 6 have been renamed rules .
In [http://dev.perl.org/perl6/doc/design/apo/A05.html Apocalypse 5], Larry Wall enumerated 20 problems with current regex culture . Among these were that Perl s regexes were too compact and cute , had too much reliance on too few metacharacters , little support for named captures , little support for grammars , and poor integration with [the] real language . He then proceeded to lay out what were then the most radical changes to the language yet.
It may be most telling that there are only five unchanged features from Perl 5 s regexes:
== Syntactic simplification ==
The parentheses (round brackets) required in control flow constructs in Perl 5 are now optional:
if is_true() { for @array { ... } }
The three dots above (...) are legal Perl 6, and are called the yadda-yadda operator . ... can be used as a placeholder for code to be inserted later. If a running program attempts to execute ... , however, an exception handling is thrown. This operator is useful for abstract methods, or for marking places where the programmer intends to insert code later.
== Chained comparisons ==
New programmers often expect chained comparisons like the following to work: if 1 = 10; }
The code above will not crash by attempting to assign a list of infinite size to the array @integers, nor will it hang indefinitely in attempting to expand the list in the for loop. Instead, it will print the integers from 0 to 10, and then continue.
Because of this behavior, the well-known Perl idiom for reading input: # Perl 5 code while () { print; } is replaced by for = { print; } This is no longer an idiom , because, unlike the Perl 5 code, which was expanded into # Perl 5 code while (defined ($_ = )) { print; } Perl 6 s lazy evaluation of for (along with a rule terminating the lazy input list <> at the End-of-file) means that the Perl 6 for loop above does not have to be subjected to any special conversion to work as expected, with the prefix = operator turns a filehandle or a file name into an iterator.
== Junctions ==
Perl 6 introduces the concept of junctions : values that are composites of other values. In the earliest days of Perl 6 s design, these were called superpositions , by analogy to the concept in quantum physics of quantum superpositions — waveforms that can simultaneously occupy several states until observation collapses them. A Perl 5 module released in 2000 by Damian Conway called [http://search.cpan.org/dist/Quantum-Superpositions/lib/Quantum/Superpositions.pm Quantum::Superpositions] provided an initial proof of concept. While at first, such superpositional values seemed like merely a programmatic curiosity, over time their utility and intuitiveness became widely recognized, and junctions now occupy a central place in Perl 6 s design.
In their simplest form, junctions are created by combining a set of values with junctive Operator (programming)s: my $even_digit = 0|2|4|6|8; # any(0, 2, 4, 6, 8) my $odd_digits = 1&3&5&7&9; # all(1, 3, 5, 7, 9) my $not_zero = none(0);
These values can be used arithmetically: my $junction = 1|2|3; $junction += 4; # junction now equals 5|6|7 $junction += (1&2); # junction now equals (6|7|8)&(7|8|9)
or in comparisons: if $grade eq any( A .. D ) { say pass }
or even in subscripting: if %person{any( first_name , nickname )} eq Joe { say What do you know, Joe }
Junctions can also be used to more richly augment the type system: class RGB_Color is Tuple[int,3] & Color { ... } sub get_tint (RGB_Color|CMYK_Color $color, num $opacity where 0|
|