What’s new in Perl 6 (The short form)
description
Transcript of What’s new in Perl 6 (The short form)
![Page 1: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/1.jpg)
What’s new in Perl 6(The short form)
What’s new in Perl 6(The short form)
![Page 2: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/2.jpg)
Important safety tipsImportant safety tips
• This is not quite final• Larry may change his mind at any
time, and has before• I don’t do syntax
![Page 3: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/3.jpg)
Biggest change firstBiggest change first
• Dereference arrow is now the dot• Concatenate is now the ~• Everyone can deal with this, I
expect• Liking it is optional
![Page 4: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/4.jpg)
The New bitsThe New bits
• Variable notation• Syntax fixes• Control flow• Regexes• Objects
![Page 5: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/5.jpg)
Variables preserve sigilsVariables preserve sigils
Perl 5$foo[1]
$foo{bar}@foo{“a”, “b”}
Perl 6@foo[1]%foo{bar}%foo{“a”, “b”}
![Page 6: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/6.jpg)
Why?Why?
• Less confusion when you write code
• Lexer doesn’t need the changed sigil to figure out what’s going on
• Since the sigil is invariant, all the dereference dots are optional
• Was: $foo->[1]
![Page 7: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/7.jpg)
Why?Why?
• Less confusion when you write code
• Lexer doesn’t need the changed sigil to figure out what’s going on
• Since the sigil is invariant, all the dereference dots are optional
• Is: $foo.[1]
![Page 8: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/8.jpg)
Why?Why?
• Less confusion when you write code
• Lexer doesn’t need the changed sigil to figure out what’s going on
• Since the sigil is invariant, all the dereference dots are optional
• Can be: $foo[1]
![Page 9: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/9.jpg)
References act as the referent
References act as the referent
@array[12] = “Foo”;$bar = @array;print $bar[12];
• Prints Foo• Works for anything the reference
points to• The reference to the array acts as
the array does• Aggregates in scalar context return
a reference
![Page 10: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/10.jpg)
Typed VariablesTyped Variables
• Typing is still optional!• Declares what a variable returns and
takes
my int @foo;
• The engine will use this to optimize for space and speed
• Can get fancier
my @foo is Matrix of int;
• Syntax still up in the air a bit
![Page 11: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/11.jpg)
Syntax FixesSyntax Fixes
• Multi-way comparisons work
$x <= 12 <= $y
• Logical operators properly propagate context
@foo = @a || @b || @c
![Page 12: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/12.jpg)
All blocks are closuresAll blocks are closures
• Well, they are• Though often it doesn’t matter• Makes it much easier to write your
own version of perl’s control structures
![Page 13: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/13.jpg)
Multiple dispatchMultiple dispatch
• Multiple subs and methods with the same name, differing only in their signature
multi sub bar (Dog $foo) {…}multi sub bar (Cat $foo) {…}
• Engine dispatches based on the runtime signature of the sub or method call
• Requires explicit declarations• Can’t happen by accident
![Page 14: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/14.jpg)
~~ is the DWIM operator~~ is the DWIM operator
![Page 15: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/15.jpg)
~~ is the smart match operator
~~ is the smart match operator
• Does The Right Thing (all 35 of them) based on the types of the left and right sides
• For example@foo ~~ @bar true if an array intersection@foo ~~ /bar/ true if any entry in array
matches%foo ~~ /bar/ true if any key matches$obj ~~ Class true if $obj isa Class
![Page 16: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/16.jpg)
Built-in switchBuilt-in switch
• Like the CPAN SWITCH module, only moreso
• Uses ~~ DWIMmery
![Page 17: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/17.jpg)
Built-in switchBuilt-in switch
given ($foo) {when 1 {print “A number”}when “a” {print “A letter”}print “Trying plan B”;when /\b\w+\b/ {print “A word”}default {print “Dunno”}
}
![Page 18: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/18.jpg)
Real exception handlingReal exception handling
• Catch exceptions with CATCH special block
• Just throw them in any try blocktry {
CATCH {warn “Help!”}print 1 / 0;
}• The try’s actually optional• You can when the exception object
![Page 19: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/19.jpg)
Named parameters in subsNamed parameters in subs
• You can now name the parameters to a subroutine or method
sub some_sub ($foo, $bar) {…}
• When you call that sub or method, you can pass them by name or position
some_sub(bar => 12, foo => 8);some_sub(“a”, “b”);
![Page 20: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/20.jpg)
Curried functionsCurried functions
• Really a shorthand for named parameters• Noted by a ^ between the sigil and variable
name• We sort them Unicodely, then substitute in• For example, they lets us kill the global $a
and $b@foo = sort {$b cmp $a} @foo
![Page 21: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/21.jpg)
Curried functionsCurried functions
• Really a shorthand for named parameters• Noted by a ^ between the sigil and variable
name• We sort them Unicodely, then substitute in• For example, they lets us kill the global $a
and $b@foo = sort {$^b cmp $^a} @foo
![Page 22: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/22.jpg)
Curried functionsCurried functions
• Really a shorthand for named parameters• Noted by a ^ between the sigil and
variable name• We sort them Unicodely, then substitute in• For example, they lets us kill the global $a
and $b@foo = sort {$^b cmp $^a} @foo
• Work in closures too$foo = {print “Hi “, $^a};$foo->(“Fred”);
• Prints Hi Fred
![Page 23: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/23.jpg)
More parens are optionalMore parens are optional
• Don’t need them for if, while, or for• Other than that, not a big deal• You can still use them
![Page 24: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/24.jpg)
For is fancierFor is fancier
• for is a bit differentfor @bar -> $baz {…}
• Works across multiple arraysfor @bar, @baz -> $x, $y {…}
• Runs until the end of the longest aggregate
• Short aggregates fill with undefs• Counts don’t have to match
for @foo, @bar -> $x, $y, $z {…}
![Page 25: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/25.jpg)
Hyper-operatorsHyper-operators
• Allow you to work on all the elements of an aggregate at once
• Noted by a »« (Or you can use >> and <<)
@total = @a »+« @b
• By default just iterates over the aggregates
• Overridable if you want to do Clever Things
@Matrix_C = @Matrix_A »*« @Matrix_B
![Page 26: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/26.jpg)
RegexesRegexes
• Lots of changes here• Regex engine is getting full lexing
and parsing support• (Everyone tries anyway, so we
might as well do it right)• Full object-oriented, inheritable,
overridable grammar system
![Page 27: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/27.jpg)
The RationaleThe Rationale
• Regexes started simply• They definitely aren’t simple any
more• The wrong things are short• Too much is wedged into that
damn (?thingie) construct• Annoying and it all looks the same• Very bad
![Page 28: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/28.jpg)
Big changesBig changes
• /x is now the default• Modifiers move to the front• Colon separates modifiers
$foo =~ s:i/\bfred\b/Barney/;
• Or inside if you’d ratherif ($bar =~ /:i \ba\w+\b/)
{ print “Word starting with A”}
• Variable-length lookbehind works
![Page 29: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/29.jpg)
Syntax changesSyntax changes
• () capturing parens• [] non-capturing parens• {} closures that execute when
control passes over them• <> mark assertions• : leads off metasyntactic things
![Page 30: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/30.jpg)
Interpolation ChangesInterpolation Changes
• $foo interpolates literally• <$foo> to interpolate as regex• @foo interpolates as an alternation• <@foo> interpolates alternations
of regexes• %foo matches against alternation
of %foo’s keys
![Page 31: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/31.jpg)
Useful new modifiersUseful new modifiers
• :p5 makes regex use perl 5 syntax• :nth maches or substitutes the nth
occurrence• :nx matches n times• :every when paired with :nth
matches/substitutes every nth occurrence
![Page 32: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/32.jpg)
Powerful enough to parse perl
Powerful enough to parse perl
• Perl 6 grammar is really a perl 6 grammar
• That means you can change it if you want
• Don’t like . for deref? Change it• It’s OO, so just change the parts
you want to• If we can parse perl, we can parse
anything
![Page 33: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/33.jpg)
New object modelNew object model
• Because almost anything’s better than what we have now
• Though it works out OK for Perl 5 and Python
• If you don’t poke it too hard…
![Page 34: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/34.jpg)
Perl 6 style objectsPerl 6 style objects
• Much more structured than perl 5 objects
• Much more compile-time restricted• Full hiding from parent and child
classes• Better dispatch control• More introspection and
overriability
![Page 35: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/35.jpg)
AttributesAttributes
• Class-specific per-object data elements
• Only visible from within methods of the defining class
• May be exposed by lvalue methods• (Though it’s still not direct access)• Fixes, more or less, the fragile
base class problem
![Page 36: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/36.jpg)
Built-in delegation supportBuilt-in delegation support
• Useful when subclassing classes with different core semantics (perl 5, Java, or .NET objects, for example)
• I have no idea how this will look• It will work, though
![Page 37: What’s new in Perl 6 (The short form)](https://reader035.fdocuments.in/reader035/viewer/2022062321/56813b00550346895da39df8/html5/thumbnails/37.jpg)
Questions?Questions?
?