|
Perl 6
Perl 6 Basics Tablet: Revision 37
Overview - Chapter: 0:History, 1:Design, 2:Basics, 3:Var, 4:Op, 5:IO, 6:{}, 7:Sub, 8:OOP, 9:Rx, 10:Meta
1st law of language redesign: Everyone wants the colon for their particular syntax. Basics doesn't mean here easy but fundamental. Which mostly translates to how to format and reformat data (numbers, strings and more). DefaultsPlease note that any Perl 6 source code is treated as unicode by default. Also use strict; and use warnings; are enabled implicitly. But the interpreter most likely also defaults to read code as Perl 5. To declare sources as Perl 6 you can write: #!/usr/bin/perl6 or just start with a keyword like module or class. StatementsUnless you use blocks, a Perl program executes one statement after another in linear progression. They have to be separated by a semicolon (;), except before and after a closing curly brace, where it is optional. $coderef = sub { fetch_data(); compute() } Spaces and IndentationPerl doesn't care about indentation. And spaces are still in many places without meaning. However these have become fewer. CommentsSingle LineLike in Perl 5 and many other languages of its league, a "#" tells the compiler to ignore the rest of the line. my $var = 'good'; # that code is boring Multi LineI many lines has to be commented, use #` followed by any pair of braces that surround the comment. $things = #`( i wonder how many of these PODEven POD is there to embedd documentation, it can be used just for inserting comments. =begin comment $= QuotingQuoting is like regular expression a sublanguage inside the main language with it's own syntactical rules. It is parsed by a special grammar as to be found in the special variable $~Q. The operator with the same name (the generic quoting operator) does almost nothing, just provides a mechanism to mark the beginning and end of text sequence. The examples in this chapter use almost every time slashes for that purpose, but any not alphanumerical character or pair of matching (bracing) character can be used as well. Q /.../ or Q |...| or Q *...* or Q "..." or Q[...] ... An extended delimiter mechanism is delivered by heredocs. Inside of these delimiters, every character will be taken literally. Any additional meaning has to be added by quoting adverbs. Most of them have a short and a long name and some of the most useful have an additional syntax that replaces them altogether with the Q operator. Single QuotesNo matter which string delimiter is chosen, sometimes he has to occur as a literal character inside the string too. In that case use single quotes with the adverb :single or one of the following aliases, from which ' ' ist maybe the best know. Its also the shortest and easy to understand, but don't allow to add other adverbs. Q :single /.../; Inside single quotes the backslash (\) "quotes" meaning: he liberates the following character from his special meaning. Or to put it simple \\ translates (or interpolates) to \ and \' to '. For anything more you need additional adverbs. 'Welcome in Larry\'s madhouse' InterpolationThe following adverbs with their short or long version allow a very fine grained definition what to interpolate. The three dots mark here optional content, mostly parameter. :b aka :backslash # control character (implies at least :q) Q :b /\t\n/; # tab and new line character Double QuotesDouble quoting combines all the previous mentioned adverbs for interpolation (also :q - implied by :b), thatswhy all the following are synonymous. Q :s, :a, :h, :f, :c, :b /.../; But further adverbs can also be added using q/.../ or qq/.../. Quote WordsWhile other quote operators return a single string item, this one can return arrays because he splits the string on any whitespace (\s aka <ws>). Q :words /.../; Q :quotewords /.../; # qw/.../ with quote protextion The second group of aliases mark a modified version, where single and double quoted strings (inside the quote) are treated as one word. Thats called quote protection. my @steps = <one "two three">; # 3 steps to success: ["one", "\"two", "three\""] Please note also that :quotewords (double pointy braces) implies :double (double quotes), which means all interpolation rules apply here also. <$pi> eq '$pi' The same pointy braces (quote operators) are also in used, when writing hash slices. HeredocsAre now normal quoted strings, only with a special delimiter, defined by the adverbs to and heredoc. Heredocs can be nested. Q :to 'EOT'; To make templates in which variables and closures are evaluated, take the normal double quote and just add the adverb for the heredoc delimiter or define with other adverbs what exactly you want to have evaluated. qq:heredoc 'EOT'; PathsPathstrings have their own quote operator. This way you get the warnings early if there is something incompatible with convention. Q :path /.../; RegexEven being a completely different language then quoting on its own (as to be defined in $~Regex and $~P5Regex), regular expressions can be built using the general quoting operator with the right adverb. Q :regex /.../ aka rx/.../ CodeThe following 3 aliases quote code that will be run immediately (on runtime) and replaced with the result. Q :exec /.../; In Perl 5 qx/.../ aka ... did a system call and not just run eval. To get that behaviour use: qqx/$cmd @args[]/ # do system call and insert result, alias to that is gone However there is yet another adverb for quasi quoting, meaning: the quoted code will be parsed and compiled into a abstract syntax tree (AST - internal representation of the compiler) during compile time. Result is the compiled AST. Parsing will be done by using the grammar stored in $~Quasi. This gets important when writing macros. Q :code /.../; Number LiteralsUnlike strings, numbers don't need quoting. But if there is a non number character in it, there will be an error. Chars of a number definition are: (0-9,.,+,-,e,E,i,_) including the radix prefixes: (0b,0o,0d,0x) and the prefix for version numbering (v). The + and can act also as operator that convert into the numerical context, which still means: take from left to right all digits and stop with the first none number character. A single underscore is allowed only between any two digits, delimiter helping readability. 3_456_789; # same as 3456789 $int = 2; Radix Prefixes0b binary - base 2, digits 0..1 General Radix Form:10<42> # same as 0d42 or 42 Scientific Notation$float = 60.2e23 # becomes automatically 6.02e24 Rational NumberTo distinguish them from a division operation, you have to groupe them with braces. (3/7) As always, .perl gives you an almost source like code formatting which results here in 3/7. Adding .nude you get (3/7), the nude source code. There are 2 different immutable value types representing both rational number. FatRat has unlimited precision and Rat has just enough to be evaled into a Real type. When you explicitly type a variable to one o them, the braces become optional. my Rat $pi_approx = 22/7; Complex Numberhave also there own immutable value type. 1+2i Version Numberv1.2.3 # okay FormattingperlThe .perl method returns a string that arranges any set of values in almost the same format, as the would be defined it source code. It's a built in Data::Dumper (pretty printer). @a.perl # evals to: [1, 2, 3, 4, 5] This works with data of any nesting depth. fmtSmall brother of sprinf that works as a method of one value. sprintfSince Algol68 known function, that inserts into a string any number of special formatted values. packFormatsmoved from core language to a module. Date and Time
Overview - Chapter: 0:History, 1:Design, 2:Basics, 3:Var, 4:Op, 5:IO, 6:{}, 7:Sub, 8:OOP, 9:Rx, 10:Meta |