|
Perl 6
Perl 6 Tutorial Part 1: Revision 32
Motivation for a friendly Perl 6 tutorialPerl 6 appears slowly before the wondering eyes of the curious, but if they grab Rakudo and the specification (aka Synopses) and try to write their first program, they often don't get too far. The language changes a bit every week and Rakudo doesn't support every command. The synopses aren't an easy read either. They are huge and contain a lot of nifty words. More written to clear edge cases to computer science students than to help the noobs to get a grasp. Therefore we need a tutorial that introduces the reader slowly into the world of Perl 6 and requires as little knowledge as possible. Of course some basic programming skills are needed and some Perl 5 wisdom is helpful, but everything else will be explained on the way. Every chapter of this tutorial is focused on one topic and contains many examples, so you can start to play with the language, which I encourage you to do. Because a start is made quickly, but it takes time to really learn Perl, and the new generation is optimized even on that. Greetings Perl 6Nearly every introduction starts with a "Hello World" example. I tried to come up with something more original, but its still the smallest, simplest program that does something joyfully useful. So shall we start with something worn out?, no something well-tried: say "Greetings, earthlings, err Perl 6 neophytes."; This first program outputs the expected text. The command say corresponds to the familiar print, but appends a line ending appropriate to the current operating system (e.g. a newline under Unix, a carriage return and newline under Windows). The print command is still available, but the new idiom is to use say. I can already hear the objections of the more experienced Perl monger: A good script begins with strict and warnings. And right they are. In Perl 5 the program should read: Perl 5: use strict; print "Greetings, earthlings, err Perl 6 neophytes.\n"; But in Perl 6 there is no need, because both pragmas are now enabled by default. This saves not only the two lines of code, which are in almost all of my programs, but it helps those who know nothing about the pragmas become aware of potential problems sooner. If necessary, one or both may be disabled with no strict or no warnings. Parentheses and Curly BracesThe next step in tutorials is often to introduce a variable, to which we assign a value and then reference: my $a = 3; We see that scalars still contain a single value (of any data type) and still use the sigil $ there's nothing more to explain. The variable is declared with my in the familiar lexical local scope, since use strict is in effect. Perhaps playing with one or two balls shouldn't be called juggling: my $a = 3; And here we already see a pleasant difference from Perl 5. The term following the 'if' may, or may not be placed in parentheses. Parentheses are used in Perl 6, only for grouping. If we have no nested structure, but only a single statement or a simple sequence of statements, they are not needed. This is true everywhere. In a similar way, an array, a variable that stores multiple values, can now be fed without parentheses. Perl 6: my @primes = 2,3,5,7; This is possible because the comma is a list operator now. The second, not quite so obvious new feature in Perl 6, which is shown in the 'if' statement above, is the absence of a final semicolon. Since Perl 1.0, the semicolon separates individual commands. Only the last command within curly brackets (a block), does not have to end with a semicolon. Also you can see (above) you no longer have to remember when following when a closing brace has a semicolon. (These rules were largely taken from C), Larry has decided that each semicolon is optional after braces from now. For 'if' blocks the same is true in Perl 5, but not as in 'eval' blocks. Perl 5: eval { print "Find all 3 differences !\n" }; Perl 6: try { say "Find all 3 differences!" }
Der dritte Unterschied ergab sich, weil 'eval' jetzt nur noch Strings auswertet und für das Ausführen von Blöcken probehalber, nun 'try' statt 'eval' zu verwenden ist. A not so free formatIn terms of formatting, Perl has become somewhat more stringent. Leading spaces cannot be inserted anywhere at will. Of course, the width of indentation, and in general the number of spaces and line breaks between two parts of an expression are irrelevant. Perl 6: $b = $a ; # assign a value But spaces and line breaks are already a divisive issue for operators, you must now pay more attention. Especially the new prefix Operator ('='), which takes the part of the popular perl5 diamond operator ('<>'), and can easily be mistaken for an assignment. The following two instructions can cause confusion: Perl 6: Perl 6: $b = =$a; # assigning a line from a stream The same character can now display multiple operators, depending on how it is surrounded by spaces: Perl 6:
++$b # Präfix-Operator, Autoinkrement vor Evaluierung
Diese Unterscheidungen sind nicht gänzlich neu, da beide Arten des Autoinkrementes Perl 5:
$array $i;
Wer seinen Quellcode so formatieren möchte, daß bestimmte Teilausdrücke oder die Perl 6:
@array\ .$i; # ein Wert aus einem Array
Blitzmerker haben aus diesem Code bereits geschlossen, daß Arrays nun immer mit noch Kommentare?
In allen bisherigen Beispielen war es zu sehen: Wie aus Perl 5, anderen Sprachen Perl 6:
my $a #( = 6; ) = 5; # es 5 wird zugewiesen
$affe# .schreit().guckt() # der schreit nicht, der guckt nur
Die letzten beiden Beispiele zeigen, daß mehrere Klammern benutzt werden dürfen. Setz es in "Anführungszeichen"
Mit der Arbeitslogik meine ich das "one-pass parsing", zu deutsch: ein Interpreter Perl 5:
/ ( # begin der capture
Der Slash ('/') im Kommentar läßt perl vermuten, hier sei die Regex zu Ende und Perl 6: say "Und der Gewinner ist %gewinner{"name"}.";
Mit Perl 5 ginge das nicht ansatzweise, weil perl die zweiten Anführungsstriche
Dies soll für einen ersten Rundgang genügen, durch die allgemeinen Grundlagen von |