Misplaced Pages

Raku (programming language): Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 19:38, 22 September 2006 editHarmil (talk | contribs)8,207 editsm Hello world: grammar← Previous edit Revision as of 13:56, 23 September 2006 edit undo86.17.154.122 (talk) Parameter passing modesNext edit →
Line 55: Line 55:
* Named * Named
* Slurpy * Slurpy
Positional parameters are the typical ordered list of parameters that most programming languages use. Nmaed parameters may be passed in any order, and are assigned to their respective parameters based on a name tag that is associated with them. Slurpy parameters are Perl 6's tool for creating ]s. Positional parameters are the typical ordered list of parameters that most programming languages use. Named parameters may be passed in any order, and are assigned to their respective parameters based on a name tag that is associated with them. Slurpy parameters are Perl 6's tool for creating ]s.


Here is an example of the use of all three parameter-passing modes: Here is an example of the use of all three parameter-passing modes:

Revision as of 13:56, 23 September 2006

Template:Beta software Perl 6 is the sixth major version of the Perl programming language, currently under development.

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 change" 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.

History

The Perl 6 design process was first announced in 2000 by Larry wall in his State of the Onion 2000 talk. At that time, the primary goals were to remove "historical warts" from the language; "easy things should stay easy, hard things should get easier, and impossible things should get hard;" a general cleanup of the internal design and APIs. The process began with a series of requests for comments or "RFCs". This process was open to all contributors, and left no aspect of the language closed to change.

Once the RFC process was complete, Wall reviewed and classified each request. He then began the process of writing the Apocalypses. While the original goal was to write one Apocolypse for each chapter of Programming Perl, it became obvious that, as each Apocalypse was written, previous Apocalypses were being invaldidated by later changes. For this reason, a set of Synopses were published, each one relatign the contents of an Apocalypse, but with any subsequent changes reflected in updates. Today, Perl 6 specification continues almost entirely within the Synopses.

There are three primary methods of communication used in the development of Perl 6 today. The first is the #perl6 IRC channel on irc.freenode.net. The second is a set of mailing lists on The Perl Foundation's servers at perl.org. The third is the subversion source code repository used by the pugs team.

Over the years, Perl 6 has undergone several alterations in its direction. The introduction of concepts from Python and Ruby were early influences, but as the pugs interpreter was written in the Haskell programming language, many functional programming influences were absorbed by the Perl 6 design team.

Implementations

Parrot is a virtual machine designed for interpreted languages, primarily for Perl 6. The self-hosting Perl 6 compiler will target (and also run on) Parrot.

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.

Pugs can execute Perl 6 code directly, as well as compile Perl 6 to JavaScript, Perl 5 or Parrot bytecode.

v6.pm is a pure Perl 5 implementation of Perl 6, making liberal use of existing CPAN modules, such as Moose and Pugs::Compiler::Rule. It aims to make the existing perl runtime a first-class virtual machine for both Perl 5 and Perl 6.

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

A major, but non-technical difference between Perl 5 and Perl 6 is that Perl 6 began as a specification. This means that Perl 6 can be re-implemented if needed, and it also means that programmers don't have to read the source code for the ultimate authority on any given feature. While Perl 5's documentation was regarded as excellent, even outside of the Perl community, 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 dynamic type system of Perl 5 has been augmented by the addition of static types. For example:

my Int $i = 0;
my Num $n = 3.142;
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 scripts, 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. Variables which do not have an explicit type default to the mutable Scalar type.

Formal Subroutine Parameter Lists

Perl 5 defined subroutines without formal parameter 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 were aliased into the elements of the array @_. If @_ were modified, the changes would be reflected in the original data.

Perl 6 introduces true formal parameters to the language. In Perl 6, a subroutine declaration looks like this:

sub do_something(Str $thing, Int $other) { ... }

As in Perl 5, the formal parameters (i.e., the variables in the parameter list) are aliases to the actual parameters (the values passed in), but by default, the aliases are constant) so they cannot be modified. They may be declared explicitly as read-write aliases for the original value or as copies using the is rw or is copy directives should the programmer require them to be modified locally.

Parameter passing modes

Perl 6 provides three basic modes of parameter passing:

  • Positional
  • Named
  • Slurpy

Positional parameters are the typical ordered list of parameters that most programming languages use. Named parameters may be passed in any order, and are assigned to their respective parameters based on a name tag that is associated with them. Slurpy parameters are Perl 6's tool for creating variadic functions.

Here is an example of the use of all three parameter-passing modes:

sub somefunction($a, $b, :$c, :$d, *@e) {...}
somefunction(1, 2, :d<3>, 4, 5, 6); # $a=1, $b=2, $d=3, @e=(4,5,6)
somefunction(:a<1>, :b<2>);         # $a=1, $b=2

Positional parameters, such as those used above are always required, unless followed by ? to indicate that they are optional. Named parameters are optional by default, but may be marked as required by adding ! after the variable name. Slurpy parameters are always optional.

Sigil invariance

In Perl 5, sigils—the punctuation characters that precede a variable name—changed depending on how the variable was used:

# Perl 5 code
my @array = (1, 2, 3);
my $element = $array; # $element equals 2

In Perl 6, sigils are invariant:

my @array = (1, 2, 3);
my $element = @array; # $element equals 2

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 could be blessed into being an object of a particular class, A blessed object could have methods invoked on it using the "arrow syntax" which would cause Perl to locate or "dispatch" an appropriate subroutine by name, and call it with the blessed variable 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 most common 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 point could be defined and used this way:

class Point is rw {
  has $.x;
  has $.y;
}
my Point $point .= new( :x<1.2>, :y<-3.7> );
# Now change x:
$point.x = 2;
say "Point is at X location: ", $point.x;

The dot replaces the arrow in a nod to the many other languages (e.g. Java, Python, and many others) that have coalesced around dot as the syntax for method invocation.

Note that the method "x" is not explicitly declared. This is called an auto-accessor. The "is rw" modifier on the class definition allows all of its public member attributes to be writable by default, using the auto-accessors.

Roles

Roles in Perl 6 take on the function of both interfaces in Java and traits in Smalltalk variant, Squeak. These are much like classes, but perform simple composition of classes rather than adding to their inheritance chain. Roles can be used to make assertions as to the interface requirements of a class, or to provide a class with behaviors.

The fundamental difference between a role and a class is that a role can never be used to define the type of an object directly, only to modify an object, class or other role.

Regular expressions

Main article: Perl 6 rules

Perl's regular expression and string-processing support has always been one of its defining features. Since Perl's pattern-matching constructs have exceeded the capabilities of formal regular expressions for some time, Perl 6 documentation will exclusively refer to them as regexes, distancing the term from the formal definition.

Perl 6 provides a superset of Perl 5 features with respect to regexes, folding them into a larger framework called "rules" which provide the capabilities of a parsing expression grammar, as well as acting as a closure with respect to their lexical scope. Rules are introduced with the rule keyword which has a usage quite similar to subroutine definition. Anonymous rules can also be introduced with the regex (or rx) keyword, or they can simply be used inline as regexps were in Perl 5 via the m (matching) or s (search and replace) operators.

In 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 'real' language".

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 syntactically valid in Perl 6 and are called the "yadda-yadda operator". If a running program attempts to execute "...", however, an exception is thrown. This operator is useful for abstract methods, or for marking places where the programmer intends to insert code later.

Chained comparisons

Perl 6 allows comparisons to "chain". That is, a sequence of comparisons such as the following are allowed:

if C(20) <= $temperature <= C(25) { say "Room temperature!" }

This is treated as if each left-to-right comparison were performed on its own, and the result is logically combined via the and operation.

Lazy evaluation

Perl 6 uses the technique of lazy evaluation of lists that has been a feature of some functional programming languages such as Haskell:

@integers = 0..Inf; # integers from 0 to infinity

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 if a limited number of slots are searched.

This simplifies many common tasks in Perl 6 including input/output operations, list transformations and parameter passing.

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 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 operators:

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 in any code that would use a normal value.

Junctions can also be used to more richly augment the type system by introducing a style of generic programming that is constrained to junctions of types:

sub get_tint ( RGB_Color|CMYK_Color $color, num $opacity) { ... }
sub store_record (Record&Storable $rec) { ... }

Autothreading

Junctions are unordered; 1|2|3 and 3|2|1 represent the same value. This lack of ordering means that the Perl 6 compiler can choose to evaluate junctive expressions in parallel. For instance, the code:

for all(@array) { ... }

would indicate to the compiler that the for loop should be run in parallel, possibly in separate threads. This feature is dubbed "autothreading"; its usage and implementation is not finalized and is a matter of debate in the development community.

Macros

In low-level languages, the concept of macros has become synonymous with textual substitution of source-code due to the widespread use of the C preprocessor. However, high-level languages such as Lisp pre-dated C in their use of macros that were far more powerful. It is this Lisp-like macro concept that Perl 6 will take advantage of. The power of this sort of macro stems from the fact that it operates on the program as a high-level data structure, rather than as simple text, and has the full capabilities of the programming language at its disposal.

A Perl 6 macro definition will look like a subroutine or method definition, and can operate on unparsed strings, an AST representing pre-parsed code, or a combination of the two. A macro definition would look like this:

macro hello($what) {
  q:code { say "Hello { {{{$what}}} }" };
}

In this particular example, the macro is no more complex than a C-style textual substitution, but because parsing of the macro parameter occurs before the macro operates on the calling code, diagnostic messages would be far more informative. However, because the body of a macro is executed at compile time each time it is used, many techniques of optimization can be employed. It is even possible to entirely eliminate complex computations from resulting programs by performing the work at compile-time.

Hello world

The hello world program is often used in demonstrating a programming language's most routine usage. In Perl 6, hello world can be written:

say "Hello world"

Though, there is more than one way to do it. say is new to Perl 6 which prints its parameters (like the print function), but with a trailing newline.

References

  1. Wall, Larry (April 2, 2001). "Apocalypse 1: The Ugly, the Bad, and the Good".
  2. Wall, Larry (2000). "State of the Onion 2000". O'Reilly Network.
  3. The Perl Foundation (2000). "About Perl6 RFCs".
  4. The Perl Foundation (2002). "Perl Development: Synopses".
  5. The Perl Foundation (2002). "Perl Development: Mailing Lists".
  6. Tang, Audrey (2005). "pugscode: Pugs".
  7. Wall, Larry (August 10, 2004). "Synopsis 1: Overview".
  8. Lee, Xah (September 25, 2005). "Perl and Python Documentations".
  9. ^ Wall, Larry (August 10, 2004). "Synopsis 2: Bits and Pieces".
  10. ^ Wall, Larry (March 21, 2003). "Synopsis 6: Subroutines".
  11. Wall, Larry (August 18, 2006). "Synopsis 12: Objects".
  12. The Software Composition Group (2003). "Traits".
  13. Wall, Larry (June 24, 2002). "Synopsis 5: Regexes and Rules".
  14. Wall, Larry (June 4, 2002). "Apocalypse 5: Pattern Matching".
  15. Wall, Larry (August 19, 2004). "Synopsis 4: Blocks and Statements".
  16. ^ Wall, Larry (September 13, 2004). "Synopsis 9: Data Structures".
  17. Conway, Damian (2002). "Quantum::Superpositions - QM-like superpositions in Perl".
  18. Adams, Rod (February 15, 2005). "Junction Values".
  19. Lamkins, David B. (2004-12-08). Successful Lisp: How to Understand and Use Common Lisp. bookfix.com.

External links

Specification

Implementations

Other

Category: