Zed Learns Perl 6 Day 1 (Or So)


#1

I’m currently learning Perl 6 mostly for fun, but also to help with my automation tasks that I have. I’m going to post my findings here for people to follow along, and possibly post some screencasts of my hacking sessions.

Today I started going through the book Think Perl 6. Personally I don’t particularly like this book since it’s not logically organized in a way that has concepts build on each other. There’s also a way to heavy focus on math problems the would exclude a large majority of people who need to learn to code. Another Perl 6 book I got seems to be just random articles by other people who do Perl 6 and want to show off impressive code. I’ll switch to that book after Think Perl mostly for completeness.

There’s also quite a few super irritating things about Think Perl. One example is the entire book up to Chapter 11 seems only have REPL code, with a teeny tiny mention of using a .pl6, .p6, or .pl ending for a file. The

Today I went through Think Perl up to Chapter 11, and most everything is fairly straight forward. Perl 6 seems to have all the things I wanted fixed in Python and Ruby, and it definitely tries to make the Perl 5 “Crypto Code” tradition die. Most of the code I’ve read and written is very readable, and Perl 6 has a feeling of making things safe to work with, while also letting you remove the safety if you want.

Perl 6 Features I Learned Today

  • Everything about variables, types, type restrictions on functions and containers, and making subset types. Very nice flexible type system that doesn’t try to beat your over the head with it.
  • Functions, recursion, but also that it doesn’t do Tail Call Optimization. Perl6 does, however, have an insanely large stack so I thought it had TCO until someone mentioned on Twitter that it didn’t. I just assumed it would crash the way Python does, but Perl just keeps on slamming functions until it runs out of RAM.
  • Hashes, arrays, and lists with some interesting features. My favorite is being able to do say [*] [1 2 3 4 5 6] to apply * (multiply) to an entire list. You can basically put an operator inside [] and it’ll run that on the whole list. Hashes are also very flexible, and there’s Set, Bag, similar data structures to deal with set situations. You also have map, reduce, and friends to do more functional programming.
  • Loops, and it is so nice to finally have a repeat-while concept. I don’t use it often, but the few times I need it Python just sucks for not having it.

Not Quite Perl

I also started researching the Not Quite Perl specification and this implementation of a little Ruby language https://github.com/perl6/nqp/blob/master/examples/rubyish/rubyish.nqp This looks very very nice, so it’s going to be one of my first things to play with after I learn more. The only thing I found is that when there’s an error the error reporting on Grammars is supposedly not very good. Can’t quite figure out why they’d do that, but this is a big jump over many other languages.

File IO Is Awesome

One thing that’s come out is how Perl 6 makes file IO so easy. You can do the usual “open file handle, do stuff, close” but they also have this .IO operator that works on any string. So you can do things like “test.txt”.IO.lines And it just opens the file text.txt and gives you the lines. No need for special path libraries, file reading, or anything else. You can do all the path stuff and file IO stuff right from the string.

Command Line Args Is Also Awesome

They let you define a MAIN function, and any arguments to the function are just treated as command line arguments with automatic help output. You just define MAIN(input_file) and there ya go, now your script takes an input_file parameter. This also works with Perl6’s multi dispatch functions, optional parameters, and keyword args. So for example, you can do this script:

multi MAIN('test', $things="nope", :$hello) {
    say "$hello $things";
}

multi MAIN('go', $power) {
    say "$power";
}

And if you run it with no arguments you get this:

perl6 test9.p6 
Usage:
  test9.p6 [--hello=<Any>] test [<things>] 
  test9.p6 go <power>

You wouldn’t believe the mountain of boilerplate code this saves on many command line scripts.

That’s it so far. Feel free to reply if you have questions about it so far.


#2

What do you think is worth learning about Perl? I don’t know anything about it. I know that it goes come preinstalled on most unix platforms. But, I don’t know how popular or useful Perl is. Especially, if I’m already comfortable with Python. Just wondering.


#3

Well this is Perl 6 so I was curious where the language ended up. I have to say it is really well designed balancing right in the sweet spot of practical and solid.


#4

intense…1987…High end for system admin. Better than C? Long time for something to become what it is :space_invader:


#5

I’ve always had a sweet spot for the practical approach taken by PERL (it’s in the name) but I haven’t used it for a good while and did find it a little obscure at times.

Now that I’m getting in to Python, which I see as a more “strategic” choice, I’m not sure I could be tempted back to PERL, especially not to versions prior to 6. Perhaps it’s a case of too liitle too late to ultimately save PERL from decline?