################################################################# # # # Perl 6 Syntax Samples - Jim's executive summary, July 2002 # # # ################################################################# # ---------------------------------------------------------------------- # Apocalypse 1 - The Ugly, the Bad, and the Good (April 2001) # This first one is an overview, and doesn't have too many specifics. # # The Perl6 interpreter will still handle Perl5 code. # A key word like "module" or "class" at the start # of the perl6 file (rather than "package") will imply Perl6. # Or something like that. # # Larry says # # "People get scared when they hear the word Apocalypse, # but here I mean it in the good sense: a Revealing. # An Apocalypse is supposed to reveal good news to good people." # # "The essence of Perl is really context sensitivity, # not just to syntactic context, but also to semantic, # pragmatic, and cultural context. This overall philosophy # is not going to change..." # # "I just want to point out that nobody should panic when # we talk about making things return objects that # didn't used to return them. Remember that any object can # define its stringify and numify overloadings to do # whatever the class likes, so old code that looks like # print scalar localtime; # can continue to run unchanged, even though localtime might # be returning an object in scalar context." # ---------------------------------------------------------------------- # Apocalypse 2 - Fundamental data types, variables, # and the context and scoping of the language (May 2001) # and a bit of Exogesis 2 # (exegesis: n. an interpretation and explanation of a text, esp. Holy Writ) # # Variables, values, and subroutines will have "properties" # in Perl6 - arbitrary pieces of information in a key/value hash. # The new keyword is "is". It's optional if an operator is expected. # The properties can be compile-time or run-time. # Some of these properties (such as dim) will be a part of the language. my int $pi is constant = 3; # "int" is optional compiler hint. my int @table is dim(366,24,60); my int $pi is shared locked constant optimize($optlevel) = 3; return 0 is true; my $snum = sub is optimize(1) { ... }; package Pet is interface; sub name is rw { ... }; # lvalue sub insert (HASH $tree is rw, int $val) { ...} # "." instead of "->" for object calls. Also returns properties. if (&name.rw) { ... } # If this subroutine has rw property,... # @foo[1] instead of $foo[1] for a single element of an array. # The new thinking is that this is a method call on the object # who's name is @foo. The funny character will always indicate # what kind of object you're invoking. @foo = (1,2,3); print @foo[0]; # In perl5 this was "%dog; $dog{color} = 'yellow';" my %dog; %dog{color} = 'yellow'; # Built in object types will include both some uppercase # things like INT and lowercase ones like "int". The difference, # like in Java, will be how close to machine-level they are. # i.e. if you want an array of a million of them, are you talking # objects or tiny pieces of data. But they will autoconvert # easily if # ---------------------------------------------------------------------- # Apocalypse 3 - Operators (October 2001), and Exegesis 3 # # Larry : "Simplification good! Oversimplification bad!". # Summary: # # Unchanged operators # prefix and postfix ++ and -- # unary !, ~, \, and - # binary ** # binary =~ and !~ # binary *, /, and % # binary + and - # binary << and >> # binary & and | # binary =, +=, -=, *=, etc. # binary , # unary not # binary and, or, and xor # # Changes to existing operators # binary -> (dereference) becomes . # binary . (concatenate) becomes _ # unary + (identity) now enforces numeric context on its argument # binary ^ (bitwise xor) becomes ~ # binary => becomes the "pair" constructor # ternary ? : bbeeccoommeess ?? :: # Enhancements to existing operators # binary .. becomes even lazier, including : for step size # binary <, >, lt, gt, ==, !=, etc. become chainable # Unary -r, -w, -x, etc. are nestable # The <> input operator are more context-aware # The logical && and || operators propagate # their context to both their operands # The x repetition operator no longer requires # listifying parentheses on its left argument in a list context. # # New operators: # unary _ is the explicit string context enforcer # binary ~~ is high-precedence logical xor # unary * is a list context specifier for parameters and # an array flattening operator for arguments # unary ^ is a meta-operator for specifying vector operations # unary := is used to create aliased variables (a.k.a. binding) # unary // is the logical 'default' operator # There's now an alias mechanism. my $age := $person[$n]{data}{personal}{time_dependent}{age}; if ($age < 12) { print "Child" } elseif ($age < 18) { print "Adolescent"} # There's a new "pair" object. my $pairRef = [1..9] => "digit"; # => is now a "pair" creator. print $pairRef.value; # prints "digit". print $pairRef.key.[3]; # prints 4. # There are many new array operations. New "vectorize" ^ print @costs[1..Inf:2] # All the odd elements of @costs. @b = @a ^+ @c; # Element by element addition, new "^+" $totalLength = @a + @c; # Numeric context, so @c is length. $ref = @a; # But a bare scalar context gives a ref. @d = [1,2,3; 4,5,6] # was [[1,2,3],[4,5,6]] @foo ^+= 1; # Add 1 to every element of @foo. # File tests nest. # This was "if (-e $file && -r $file && -w $file)" in perl5. if (-w -r -e $file) { ... } # There's a new "default" operator. # In perl5 " $offset = 1 unless $defined $offset" $offset //= 1; # left side if defined else right side. # String concatenation is now "_", but only if with spaces. $name = getTitle _ getName; # was "getTitle() . getName()" # ":" can now be used as an "adverb", turning almost anything # into a trinary operator. print $handle[2]: @args; # The old ?: is now spelled ??::. (Too many other uses for ?, :) my $s = $string.length > 1 ?? 's' :: ''; # ---------------------------------------------------------------------- # Apocalypse 4 - Syntax (January 2002) # and Exegesis 4 - "Now Witness the Power of this Fully *Operational* # Control Structure" # # subtitled "All About Blocks". # # # Any curly brace with a space in front of it is always a block, # not a subscript. So %hash{foo} must not have a space. # # This means that typical control statements no longer require (). if $foo { ... } elsif $bar { ... } else { ... } while $more { ... } for 1..10 { ... } # There is anow a "switch statement" which is spelled like this: given EXPR { when EXPR { ... } when EXPR { ... } ... } # Exceptions and throw/catch are built-in. my class X::Alarm is Exception { } try { throw X::Alarm "a message", tag => "ABC.1234"; CATCH { when X::Alarm { ... } when Error::DB, Error::IO { ... } } } # The "for" keyword will always mean "foreach", and must take # an array. If you want to do the C-style (i=0; i<10; i++) # the new keyword is "loop". The list of what to loop over # is optional. loop { ... last unless CONDITION; } loop ($i=0; $i<10; $i++) { ... } # ---------------------------------------------------------------------- # Apocalypse 5 - Regexes (June 2002), and # Synopsis 5 # # Many changes. The regex engine now has named "rules" # which can call each other, like subroutines. The various # types of grouping have been cleaned up and redefined. # And it's much easier to embed perl within a regex. # # I don't really follow all this yet, I must confess. # We'll see how it all falls out eventually. # # Larry: "Regular expression culture is a mess, and # I share some of the blame for making that way. # Since my mother always told me to clean up my # own messes, I suppose I'll have to do just that." # # (...) # always delimits a capturing group # [...] # always delimits a non-capturing group, NOT char class # {...} # always delimits a closure, i.e. perl code # <...> # always delimits an assertion # :... # always introduces a metasyntactic token ( was /x or /x) # # What was /x "extended" behavior is now the default. # whitespace is not matched literally by default, # # introduces comments by default # Old New # /pat pat/ / pat \ pat / # must match whitespace literally # /^pat$/ /^pat\n$/ # ^ and $ are string start/end # /^pat^/m /^^pat$$/ # ^^ and $$ are line start/end # (??{ code }) { code } # call perl code, ignore return # { code or fail } # use code as an assertion # # [a-z] <[a-z]> # [^[:alpha]] <-alpha> # \h # horizontal whitespace # \v # vertical whitespace # \H # anything BUT horizontal white # \V # anything BUT vertical white # Other examples s:w:i:e /foo/bar/; # substitute "foo" for "bar". # :words :ignorecase :each # The modifiers come before, not after. s:3rd /foo/bar/; # substitute 3rd instance of "foo" for "bar". s:3x /foo/bar/; # substitute first 3 "foo"s for "bar"s. # Keyword "let" for hypothetical assignment. / (\d+) {let $num := $1} (+)/ # Named "rules" analogous to subroutones. rule ident { [|_] \w* } # Define a maed regex rule # @ids = grep //, @strings; # And use it later on. ######################################################################## # # # The bottom line ... we'll see how it all turns out. # # For my own use, the array syntax enhancements and optional type # declarations could be very cool, if it all works out as planned. # # This summary has emphasized the differences. But it's clear # from the Exogesis examples that most of Perl will still be Perl. # # So if you like it now, you probably still will. # And if you don't, then you still won't. # ########################################################################