The Perl 6 Language Jonathan Worthington UKUUG Spring 2007 - - PowerPoint PPT Presentation

the perl 6 language
SMART_READER_LITE
LIVE PREVIEW

The Perl 6 Language Jonathan Worthington UKUUG Spring 2007 - - PowerPoint PPT Presentation

The Perl 6 Language Jonathan Worthington UKUUG Spring 2007 Conference The Perl 6 Language Everyone loves Perl 5, because It's great for hacking up one-off scripts Can write one-liners directly at the command line Really good at


slide-1
SLIDE 1

The Perl 6 Language

Jonathan Worthington UKUUG Spring 2007 Conference

slide-2
SLIDE 2

The Perl 6 Language

Everyone loves Perl 5, because…

It's great for hacking up one-off scripts Can write one-liners directly at the

command line

Really good at extracting data in a wide

range of formats…

…and spitting it out again in some other

form, or generating reports on it

Possible to build large systems too

slide-3
SLIDE 3

The Perl 6 Language

Perl 6: the next step

A ground-up redesign of the language A partial prototype interpreter is

available to play with today

Aims to make the easy things even

easier, and the hard things less painful

Much stronger when it comes to

building large systems

But still the Perl we know and love

slide-4
SLIDE 4

The Perl 6 Language

Overview

This talk: an introduction to writing

programs in Perl 6

The main message: Perl 6 rocks! Tomorrow’s talk: what makes up Perl 6,

what to expect you’ll be deploying, migration issues, the future of CPAN

The main message: don’t panic!

slide-5
SLIDE 5

The Perl 6 Language

Hello, world!

slide-6
SLIDE 6

The Perl 6 Language

Hello, world!

In Perl 5: Writing \n at the end of every print

statement is very common

In Perl 6: the new say keyword saves

you from having to do that

An easy thing made easier

print "Hello, world!\n"; say "Hello, world!";

slide-7
SLIDE 7

The Perl 6 Language

Variables

slide-8
SLIDE 8

The Perl 6 Language

Variables

As in Perl 5, three container types:

# Scalars hold one value my $name = "Jonathan"; # Arrays hold many values my @fave_foods = "Curry", "Pizza", "Beef"; # Hashes hold many key/value pairs my %opinions = ( Perl => ‘Awesome’, Vista => ‘Suckful’, Ale => ‘Tasty’ );

slide-9
SLIDE 9

The Perl 6 Language

Variables

Unlike Perl 5, sigils are invariant

## Arrays – always use @ say @fave_foods[1]; # Pizza @fave_foods[3] = “Yorkshire Puddings“; ## Hashes – always use % # <...> for constant keys say %opinions<Ale>; # Tasty %opinions<Switzerland> = “Beautiful“; # Curly brackets allow variables there too my $what = "Manchester"; %opinions{$what} = "Rainy";

slide-10
SLIDE 10

The Perl 6 Language

Iteration

slide-11
SLIDE 11

The Perl 6 Language

Iterating Over An Array

Iteration = doing something for each

thing in the array

The bit between the curly braces is

done for each thing in the array

  • > $name means “declare $name and

put the current thing into it”

for @fave_foods -> $food { say "Jonathan likes to eat $food"; }

slide-12
SLIDE 12

The Perl 6 Language

Iterating Over A Hash

Can iterate over all of the keys… Or all of the values with .values, or

both at the same time with .kv

for %opinions.keys -> $what { say “Jonathan has a view on $what“; } # Print environment variables for %*ENV.kv -> $var, $value { say “$var = $value"; }

slide-13
SLIDE 13

The Perl 6 Language

Iterating Over Many Arrays At Once

More generally, can iterate over two or

more arrays at a time

Use the zip function to interleave the

elements of two or move lists

for zip(@ids; @logins; @groupids)

  • > $id, $login, $groupid {

say "$login:x:$id:$groupid:..."; }

slide-14
SLIDE 14

The Perl 6 Language

Conditionals

slide-15
SLIDE 15

The Perl 6 Language

Save two keystrokes!

Fairly typical if…else style construct;

note no parentheses needed around the condition

if $x == 42 { say "It's the answer!"; } elsif $x == 7 { say "It's perfect!"; } else { say "It's some other number."; }

slide-16
SLIDE 16

The Perl 6 Language

Junctions

Allow you to test a variable against

many conditions more easily

The equivalent Perl 5 is

unless $input eq 'y' | 'n' | 'c' { print "(y)es/(n)o/(c)ancel? "; } unless ($input eq 'y' || $input eq 'n' || $input eq 'c') { print "(y)es/(n)o)/(c)ancel? "; }

slide-17
SLIDE 17

The Perl 6 Language

Junctions

You can build junctions from an array

too

There are other types of junction

all & true for all elements

  • ne

^ true for exactly one element none true for no elements

my @bad_ext = ('vbs', 'js', 'exe', 'reg'); if lc($file_ext) eq any(@bad_ext) { say "$file_ext files not allowed"; }

slide-18
SLIDE 18

The Perl 6 Language

Chained Comparisons

Now it's easier to check if a user input

is sandwiched between two values

if 0 <= $score_pc <= 100 { say "You can't score $score_pc"; }

slide-19
SLIDE 19

The Perl 6 Language

I/O

slide-20
SLIDE 20

The Perl 6 Language

Reading Entire Files

Reading in an entire file is now as

simple as

Or to get an array with an element for

each line in the file

Reads the whole file in one go – very

handy, but be careful when dealing with big files!

my $file_content = slurp("filename.txt"); my @lines = slurp("filename.txt");

slide-21
SLIDE 21

The Perl 6 Language

Iterating Over Files Line By Line

Use open to get a file handle; use :r

to indicate we want to read

Iterate over the file's lines using for Close the file when you're done

my $fh = open "file.txt" :r; for =$fh -> $line { ... } $fh.close();

slide-22
SLIDE 22

The Perl 6 Language

Reading From STDIN

All global variables start with $* The STDIN file handle is in $*IN

Iteration the same as on the last

slide…

Can read a single line too

for =$*IN -> $line ... } my $input = =$*IN;

slide-23
SLIDE 23

The Perl 6 Language

Powerful List Processing

slide-24
SLIDE 24

The Perl 6 Language

List Processing

Perl 6 has made some big advances

when it comes to doing operations involving lists (arrays) of data

Will make computing various statistics,

such as sums and averages, much neater

In general, implemented as meta-

  • perators: they add meaning to all

existing operators

slide-25
SLIDE 25

The Perl 6 Language

Reduction Operators

To form the reduction operator,

surround any infix operator by […]

# Add all elements of the array my $sum = [+] @values; # Multiply together numbers from 1 to $n my $factorial_n = [*] 1..$n; # Check if the list is sorted ascending if [<=] @list { say "Sorted ascending"; }

slide-26
SLIDE 26

The Perl 6 Language

Hyper Operators

Used to perform an operation per

element of an array

This is similar to a loop that takes

elements 0 from @a and @b, adds them and puts the result in element 0 of @c

Gives permission for the operation on

different elements to be parallelized => good for the Concurrent Future

my @c = @a >>+<< @b;

slide-27
SLIDE 27

The Perl 6 Language

Cross Operators

Forms every possible permutation of

two or more lists

This is a special case; can stick an

  • perator in-between two Xs

(1,2) X (3,4) # ((1,3),(1,4),(2,3),(2,4)) # If @user_facts contains words relating to # a user, can concatenate all possible # combinations of them together – test for # weak passwords. :-) my @guesses = @user_facts X~X @user_facts;

slide-28
SLIDE 28

The Perl 6 Language

Powerful Text Parsing

slide-29
SLIDE 29

The Perl 6 Language

From Regex To Rules And Grammars

Regex in Perl 5 are very powerful for

parsing

However, they are based on regular

languages

Makes parsing some things,

particularly anything recursive (e.g. bracketed data) tricky

Some find the syntax a little arcane

slide-30
SLIDE 30

The Perl 6 Language

Grammars

Grammars make defining how to parse

things easier

Encourages re-use

grammar ConfigFile { token File { <Section>+ } token Section { <Heading> <Entry>* } token Heading { <'['> (\w+) <']'> \n } token Entry { (\w+) <ws> = <ws> (\w+) \n+ } }

slide-31
SLIDE 31

The Perl 6 Language

Final Thoughts

slide-32
SLIDE 32

The Perl 6 Language

Play With Perl 6 Today!

In your web browser

http://run.pugscode.org/

Source code to Pugs (a partial Perl 6

compiler) is on the CD or get the latest version from

http://www.pugscode.org/

Perl 6 FAQ at

http://programmersheaven.com/2/Perl6-FAQ

slide-33
SLIDE 33

The Perl 6 Language

Conclusion

Perl 5 aims to make the easy things

easy and hard things possible

Perl 6 aims to make the easy things

easier and the hard things less painful

I think Perl 6 will be…

slide-34
SLIDE 34

Beautiful

slide-35
SLIDE 35

Cool

slide-36
SLIDE 36

Cool A little crazy!

slide-37
SLIDE 37

The Perl 6 Language

Thank you!

slide-38
SLIDE 38

The Perl 6 Language

Questions?