Introdução ao Perl 6

136
Introdução ao Perl 6 @that_garu

Transcript of Introdução ao Perl 6

Page 1: Introdução ao Perl 6

Introdução ao Perl 6@that_garu

Page 2: Introdução ao Perl 6

Perl 6

Page 3: Introdução ao Perl 6

Perl, reescrito pela própria comunidade.

Page 4: Introdução ao Perl 6

Um novo Perl

Page 5: Introdução ao Perl 6

mais moderno

Page 6: Introdução ao Perl 6

mais amigável

Page 7: Introdução ao Perl 6

mais poderoso

Page 8: Introdução ao Perl 6

mais correto

Page 9: Introdução ao Perl 6

mais seguro

Page 10: Introdução ao Perl 6

Instalando

Page 11: Introdução ao Perl 6

git clone https://github.com/tadzik/rakudobrew ~/.rakudobrew export PATH=~/.rakudobrew/bin:$PATH rakudobrew init rakudobrew build moar

rakudobrew build-panda panda install Linenoise

Page 12: Introdução ao Perl 6

Números

Page 13: Introdução ao Perl 6

6

Page 14: Introdução ao Perl 6

say 6

Page 15: Introdução ao Perl 6

say 66

Page 16: Introdução ao Perl 6

say 6.WHAT

Page 17: Introdução ao Perl 6

say 6.WHAT(Int)

Page 18: Introdução ao Perl 6

6.0

Page 19: Introdução ao Perl 6

6.029e4

Page 20: Introdução ao Perl 6

say 6.029e4.WHAT

say 6.WHAT

say 6.0.WHAT

(Int)

(Rat)

(Num)

Page 21: Introdução ao Perl 6

say 7/2

Page 22: Introdução ao Perl 6

say 7/23.5

Page 23: Introdução ao Perl 6

Ruby

puts 7/2

Python 2

print 7/2

JavaScript

console.log(7/2)

C#include <stdio.h> int main (void) { printf(“%f”, 7/2); return 0; }

Page 24: Introdução ao Perl 6

Ruby

puts 7/2

3

Python 2

print 7/2

3

JavaScript

console.log(7/2)

3.5

C

WARNING: argument has type ‘int' 0.0000

#include <stdio.h> int main (void) { printf(“%f”, 7/2); return 0; }

Page 25: Introdução ao Perl 6

Ruby

puts 7/2

3

Python 2

print 7/2

3

JavaScript

console.log(7/2)

3.5

C#include <stdio.h> int main (void) { printf(“%d”, 7/2); return 0; }

3

Page 26: Introdução ao Perl 6

say .1 + .2 - .3

Page 27: Introdução ao Perl 6

say .1 + .2 - .30

Page 28: Introdução ao Perl 6

Ruby

puts 0.1 + 0.2 - 0.3

Python 2

print .1 + .2 - .3

JavaScript

console.log(.1 + .2 - .3)

Perl 5

say .1 + .2 - .3

Page 29: Introdução ao Perl 6

Ruby

puts 0.1 + 0.2 - 0.3

5.551115123125783e-17

Python 2

print .1 + .2 - .3

5.55111512313e-17

JavaScript

console.log(.1 + .2 - .3)

Perl 5

5.551115123125783e-17

say .1 + .2 - .3

5.55111512312578e-17

Page 30: Introdução ao Perl 6

say 7.2.numerator36say 7.2.denominator5say 7.2.nude(36 5)

say 36/57.2

Page 31: Introdução ao Perl 6

Números

Page 32: Introdução ao Perl 6

Page 33: Introdução ao Perl 6

say ೬.WHAT(Int)

Page 34: Introdução ao Perl 6

say ೬ + 17

Page 35: Introdução ao Perl 6
Page 36: Introdução ao Perl 6

Strings

Page 37: Introdução ao Perl 6

say “Alô, YAPC"

Page 38: Introdução ao Perl 6

say 'Alô, YAPC'

Page 39: Introdução ao Perl 6

say 「Alô, YAPC」

Page 40: Introdução ao Perl 6

say q|Alô, YAPC|

Page 41: Introdução ao Perl 6

say ‘YA’ ~ ‘PC’YAPC

Page 42: Introdução ao Perl 6

say uc(‘yapc’)YAPC

Page 43: Introdução ao Perl 6

say ‘yapc’.ucYAPC

Page 44: Introdução ao Perl 6

say “\c[DROMEDARY CAMEL]"

🐪

Page 45: Introdução ao Perl 6

say ‘🐪’🐪

Page 46: Introdução ao Perl 6

say ‘🐪’.chars1

Page 47: Introdução ao Perl 6

‘YAPC::BR’.ends-with(‘BR')

True

Page 48: Introdução ao Perl 6

Variáveis

Page 49: Introdução ao Perl 6

my $var

Page 50: Introdução ao Perl 6

say $var.WHAT(Any)

Page 51: Introdução ao Perl 6

my Int $var

Page 52: Introdução ao Perl 6

say $var.WHAT(Int)

Page 53: Introdução ao Perl 6

my $resposta = 42

Page 54: Introdução ao Perl 6

say $resposta.WHAT(Int)

Page 55: Introdução ao Perl 6

my @paises = ( ‘Brasil’, ‘Chile’);

Page 56: Introdução ao Perl 6

my @paises = ‘Brasil’, ‘Chile’;

Page 57: Introdução ao Perl 6

my @paises = qw{Brasil Chile};

Page 58: Introdução ao Perl 6

my @paises = <Brasil Chile>;

Page 59: Introdução ao Perl 6

my @paises = << Brasil Chile “Reino Unido">>;

Page 60: Introdução ao Perl 6

say @paises.elems3

Page 61: Introdução ao Perl 6

say @paises[0]Brasil

Page 62: Introdução ao Perl 6

say @paises[*-1]Reino Unido

Page 63: Introdução ao Perl 6

say @paises[^2](Brasil Chile)

Page 64: Introdução ao Perl 6

say 0...10(0 1 2 3 4 5 6 7 8 9 10)

Page 65: Introdução ao Perl 6

say 0,2...10(0 2 4 6 8 10)

Page 66: Introdução ao Perl 6

say 2, 4, 8...100(2 4 8 16 32 64)

Page 67: Introdução ao Perl 6

my @lazy = 2, 4, 8...*; say @lazy[20]2097152

Page 68: Introdução ao Perl 6

my @primes = grep { .is-prime }, 1..*;say @primes[^10]

(2 3 5 7 11 13 17 19 23 29)

Page 69: Introdução ao Perl 6

my @fib = 0, 1, * + * ... *;say @fib[^10](0 1 1 2 3 5 8 13 21 34)

Page 70: Introdução ao Perl 6

my %capitais = ( ‘Brasil' => ‘Brasília', ‘Chile' => ‘Santiago’,);

Page 71: Introdução ao Perl 6

my %capitais = ‘Brasil' => ‘Brasília', ‘Chile' => ‘Santiago’,;

Page 72: Introdução ao Perl 6

say %capitais.elems

2

Page 73: Introdução ao Perl 6

say %capitais{‘Brasil'}

Brasília

Page 74: Introdução ao Perl 6

say %capitais<Brasil>

Brasília

Page 75: Introdução ao Perl 6

say %capitais< Brasil Chile >

(Brasília Santiago)

Page 76: Introdução ao Perl 6

%capitais<Brasil>:exists

True

Page 77: Introdução ao Perl 6

Condicionais

Page 78: Introdução ao Perl 6

if / elsif / else / unless given - when

Page 79: Introdução ao Perl 6

if ($x < 10) { ... }

Page 80: Introdução ao Perl 6

if $x < 10 { ... }

Page 81: Introdução ao Perl 6

if 5 < $x < 10 { ... }

Page 82: Introdução ao Perl 6

say $x unless 5 < $x < 10

Page 83: Introdução ao Perl 6

Laços

Page 84: Introdução ao Perl 6

foreach

Page 85: Introdução ao Perl 6

while ($x < 10) { ... }

Page 86: Introdução ao Perl 6

while $x < 10 { ... }

Page 87: Introdução ao Perl 6

until $x < 10 { ... }

Page 88: Introdução ao Perl 6

while (1) { ... }

Page 89: Introdução ao Perl 6

loop { ... }

Page 90: Introdução ao Perl 6

loop (my $x = 0; $x < 10; $x++) { ...}

Page 91: Introdução ao Perl 6

for @capitais -> $cidade { ...}

Page 92: Introdução ao Perl 6

for ^10 -> $n1, $n2 { ...}

Page 93: Introdução ao Perl 6

I/O

Page 94: Introdução ao Perl 6

my $nome = prompt “nome:"

Page 95: Introdução ao Perl 6

my $dados = slurp ‘arquivo.txt'

Page 96: Introdução ao Perl 6

spurt ‘arquivo.txt’, $dados

Page 97: Introdução ao Perl 6

my @linhas = ‘arquivo.txt’.IO.lines

Page 98: Introdução ao Perl 6

my $fh = open ‘arquivo.txt’;for $fh.lines -> $line { say $line if $line ~~ /abc/;}

Page 99: Introdução ao Perl 6

my $fh = open ‘arquivo.txt’;for $fh.lines { .say if /abc/; }

Page 100: Introdução ao Perl 6

my $fh = open ‘arquivo.txt’;.say if /abc/ for $fh.lines;

Page 101: Introdução ao Perl 6

funções

Page 102: Introdução ao Perl 6

sub oi ($nome) { return “oi, $nome!";}

Page 103: Introdução ao Perl 6

sub oi (Str $nome) { return “oi, $nome!";}

Page 104: Introdução ao Perl 6

sub oi (Str $nome = 'Anônimo') { return “oi, $nome!";}

Page 105: Introdução ao Perl 6

sub oi (Str $nome where *.chars > 0) { return “oi, $nome!"; }

Page 106: Introdução ao Perl 6

subset NonEmptyStr of Str where *.chars > 0;sub oi (NonEmptyStr $nome) { return “oi, $nome!"; }

Page 107: Introdução ao Perl 6

subset NonEmptyStr of Str where *.chars > 0;sub oi (NonEmptyStr $nome) is cached { return “oi, $nome!"; }

Page 108: Introdução ao Perl 6

subset NonEmptyStr of Str where *.chars > 0;sub oi (NonEmptyStr $nome) returns Str { return “oi, $nome!"; }

Page 109: Introdução ao Perl 6

subset NonEmptyStr of Str where *.chars > 0; sub oi (NonEmptyStr $nome) is cached returns Str { return “oi, $nome!"; }

Page 110: Introdução ao Perl 6

sub dobra (Int $num) { return $num * 2;}

Page 111: Introdução ao Perl 6

multi dobra (Int $num) { return $num * 2;}

Page 112: Introdução ao Perl 6

multi dobra (Int $num) { return $num * 2;}multi dobra (Str $palavra) { return $palavra x 2;}

Page 113: Introdução ao Perl 6

fizzbuzzsem condicionais?

Page 114: Introdução ao Perl 6

sub fizzbuzz($n) { multi case($, $) { $n } multi case(0, $) { “fizz" } multi case($, 0) { “buzz" } multi case(0, 0) { "fizzbuzz" } case $n % 3, $n % 5;}

Page 115: Introdução ao Perl 6

classes

Page 116: Introdução ao Perl 6

class Point { has $.x = 0; has $.y = 0;

method Str { “[$.x,$.y]” }}

my $ponto = Point.new(x => 4,y => 7); say $ponto.x; # 4 say $ponto.y; # 7 say “$ponto" # [4,7]

Page 117: Introdução ao Perl 6

class Point { has $.x = 0; has $.y = 0;

method Str { “[$.x,$.y]” } method set (:$x = $.x, :$y = $.y) { $!x = $x; $!y = $y; } }

my $ponto = Point.new(x => 4,y => 7); say “$ponto" # [4,7]$ponto.set( y => -1 ); say “$ponto" # [4,-1]

Page 118: Introdução ao Perl 6

class Point { has Real $.x = 0; has Real $.y = 0;

method Str { “[$.x,$.y]” } method set (:$x = $.x, :$y = $.y) { $!x = $x; $!y = $y; } }

my $ponto = Point.new(x => 4,y => 7); say “$ponto" # [4,7]$ponto.set( y => -1 ); say “$ponto" # [4,-1]

Page 119: Introdução ao Perl 6

class Point { has Real $.x is rw = 0; has Real $.y is rw = 0;

method Str { “[$.x,$.y]” }}

my $ponto = Point.new(x => 4,y => 7); say “$ponto" # [4,7]$ponto.y = -1;say “$ponto" # [4,-1]

Page 120: Introdução ao Perl 6

class Point { subset Limitado of Real where -10 <= * <= 10; has Limitado $.x is rw = 0; has Limitado $.y is rw = 0;

method Str { “[$.x,$.y]” }}

my $ponto = Point.new(x => 4,y => 7); say “$ponto" # [4,7]$ponto.y = -1;say “$ponto" # [4,-1]

Page 121: Introdução ao Perl 6

class Point { subset Limitado of Real where -10 .. 10; has Limitado $.x is rw = 0; has Limitado $.y is rw = 0;

method Str { “[$.x,$.y]” }}

my $ponto = Point.new(x => 4,y => 7); say “$ponto" # [4,7]$ponto.y = -1;say “$ponto" # [4,-1]

Page 122: Introdução ao Perl 6

class Point { subset Limitado where -10.0 .. 10.0; has Limitado $.x is rw = 0; has Limitado $.y is rw = 0;

method Str { “[$.x,$.y]” }}

my $ponto = Point.new(x => 4,y => 7); say “$ponto" # [4,7]$ponto.y = -1;say “$ponto" # [4,-1]

Page 123: Introdução ao Perl 6

class Point { subset Limitado where -10.0 .. 10.0; has Limitado $.x is rw = die ‘x is required'; has Limitado $.y is rw = die ‘y is required';

method Str { “[$.x,$.y]” }}

my $ponto = Point.new(x => 4,y => 7); say “$ponto" # [4,7]$ponto.y = -1;say “$ponto" # [4,-1]

Page 124: Introdução ao Perl 6

class Point { subset Limitado where -10.0 .. 10.0; has Limitado $.x is rw = die ‘x is required'; has Limitado $.y is rw = die ‘y is required'; }

my $ponto = Point.new(x => 4,y => 7); $ponto.y = -1;say $ponto.perl # Point.new(x => 4, y => -1)

Page 125: Introdução ao Perl 6

class Point { subset Limitado where -10.0 .. 10.0; has Limitado $.x is rw = die ‘x is required'; has Limitado $.y is rw = die ‘y is required'; }

Perl 6package Point { use Carp; use overload '""' => \&Str, fallback => 1; sub _pointlimit { my ( $class, $num ) = @_; unless ( $num >= -10 && $num <= 10 ) { croak "..."; } } sub new { my ( $class, $x, $y ) = @_; my $self = bless { x => undef, y => undef } => $class; $self->x($x); $self->y($y); return $self;

} sub x { my ( $self, $x ) = @_; $self->_pointlimit ($x); $self->{x} = $x;

} sub y { my ( $self, $y ) = @_; $self->_pointlimit ($y); $self->{y} = $y; } sub Str { my $self = shift; return sprintf "[%f,%f]" => $self->x, $self->y; }}

Perl 5 (core)

Page 126: Introdução ao Perl 6

class Point { subset Limitado where -10.0 .. 10.0; has Limitado $.x is rw = die ‘x is required'; has Limitado $.y is rw = die ‘y is required'; }

Perl 6package Point { use Moose; use overload '""' => \&Str, fallback => 1;

use Moose::Util::TypeConstraints; subtype "PointLimit" => as 'Num' => where { $_ >= -10 && $_ <= 10 } => message { "$_ must be a Num between -10 and 10, inclusive" };

has [qw/x y/] => ( is => 'rw', isa => 'PointLimit', required => 1, );

sub Str { my $self = shift; return sprintf "[%f,%f]" => $self->x, $self->y; }}

Perl 5 (Moose)

Page 127: Introdução ao Perl 6

class Point { subset Limitado where -10.0 .. 10.0; has Limitado $.x is rw = die ‘x is required'; has Limitado $.y is rw = die ‘y is required'; }

Perl 6#include <iostream> #include <sstream> class Point { double x_, y_; public: Point (double x, double y) : x_(constrain(x)), y_(constrain(y)) {} double x () const { return x_; } double y () const { return y_; } void x (double num) { x_ = constrain(num); } void y (double num) { y_ = constrain(num); } friend std::ostream & operator<< (std::ostream & stream, const Point & point) { stream << '[' << point.x() << ',' << point.y() << ']'; return stream; } private: static double constrain (double num) { if (num < -10 || num > 10) { std::stringstream ss; ss << "'" << num << "' is not a real number between -10 and 10, inclusive"; throw(ss.str()); }

return num; }

};

C++

Page 128: Introdução ao Perl 6

class Point { subset Limitado where -10.0 .. 10.0; has Limitado $.x is rw = die ‘x is required'; has Limitado $.y is rw = die ‘y is required'; }

Perl 6public class Point { private static final double X_MIN = -10.0, X_MAX = 10.0; private static final double Y_MIN = -10.0, Y_MAX = 10.0; private double x, y;

public Point(double x, double y) throws IllegalArgumentException { setX(x); setY(y); }

public double getX() { return x; } public double getY() { return y; } public void setX(double x) throws IllegalArgumentException { if (x < X_MIN || x > X_MAX) throw new IllegalArgumentException("..."); this.x = x; } public void setY(double y) throws IllegalArgumentException { if (y < Y_MIN || y > Y_MIN) throw new IllegalArgumentException("..."); this.y = y; }

@Override public String toString() { return String.format("[%.1f,%.1f]", x, y); } }

Java

Page 129: Introdução ao Perl 6

class Point { subset Limitado where -10.0 .. 10.0; has Limitado $.x is rw = die ‘x is required'; has Limitado $.y is rw = die ‘y is required'; }

Perl 6class PointLimit: def __init__(self, name): self.name = name

def __get__(self, point, owner): return point.__dict__.get(self.name) def __set__(self, point, value): if not -10 < value < 10: raise ValueError('Value %d is out of range' % value) point.__dict__[self.name] = value

class Point: x = PointLimit('x'); y = PointLimit('y');

def __init__(self, x, y): self.x = x self.y = y

def __str__(self): return "[%f,%f]" % (self.x, self.y)

Python 3

Page 130: Introdução ao Perl 6

class Point { subset Limitado where -10.0 .. 10.0; has Limitado $.x is rw = die ‘x is required'; has Limitado $.y is rw = die ‘y is required'; }

Perl 6function definePointLimit(point, name) { Object.defineProperty(point, name, { set: function (value) { if (value < -10 || value > 10) throw 'Value ' + value + ' is out of range'; point['_' + name] = value; }, get: function () { return point['_' + name]; } });}

function Point(x, y) { definePointLimit(this, 'x'); definePointLimit(this, 'y');

this.x = x; this.y = y;}

Point.prototype.toString = function() { return '[' + this.x + ',' + this.y + ']'; }

JavaScript

Page 131: Introdução ao Perl 6

class Point { subset Limitado where -10.0 .. 10.0; has Limitado $.x is rw = die ‘x is required'; has Limitado $.y is rw = die ‘y is required'; }

Perl 6class Point POINT_RANGE = -10..10

attr_constrained x: POINT_RANGE, y: POINT_RANGE

def initialize @x = 0 @y = 0 end

def to_s "[#{x},#{y}]" end

private def self.attr_constrained(attrs_and_constraints) attrs_and_constraints.each do |attr, constraint| attr_reader attr define_method(:"#{attr}=") do |value| raise "#{value} is not a valid value for #{attr}" \ unless constraint === value instance_variable_set :"@#{attr}", value end end end end

Ruby

Page 132: Introdução ao Perl 6

class Point { subset Limitado where -10.0 .. 10.0; has Limitado $.x is rw = die ‘x is required'; has Limitado $.y is rw = die ‘y is required'; }

Perl 6package point

import "fmt" import "errors"

type Point struct { x, y float64 }

func NewPoint(x, y float64) (*Point, error) { p := &Point{x, y} if !IsValid(x) { return nil, errors.New("Point x out of range!”) } if !IsValid(y) { return nil, errors.New("Point y out of range!”) } return p, nil}

func IsValid(p float64) (result bool) { if p >= -10 && p <= 10 { result = true } return}

func (p *Point) String() string { return fmt.Sprintf("[%v,%v]", p.x, p.y) }func (p *Point) SetX(x float64) (float64, error) { ox := p.x if !IsValid(x) { return ox, errors.New("Point out of range!") } return ox, nil}

func (p *Point) SetY(y float64) (float64, error) { oy := p.y if !IsValid(y) { return oy, errors.New("Point out of range!") } return oy, nil} func (p *Point) GetX() float64 { return p.x }func (p *Point) GetY() float64 { return p.y }

Go

Page 133: Introdução ao Perl 6

CLIasync

gramáticas

exceções MOP

Native Calling

junctions

Page 134: Introdução ao Perl 6

docs.perl6.org

Page 135: Introdução ao Perl 6

Obrigado! Dúvidas?

@that_garu

Page 136: Introdução ao Perl 6

Créditos e informações adicionais

Carl Mäsak - fizzbuzz.p6: https://gist.github.com/masak/b9371625ad85cfe0faba

Jonathan Worthington - Perl 6 hands-on tutorial http://jnthn.net/papers/2015-spw-perl6-course.pdf

Curtis “Ovid" Poe - Perl 6 for mere mortals http://www.slideshare.net/Ovid/perl-6-for-mere-mortals