You may also specify the return type in the parameter (remember the type system?) The above could also have been started with:

sub cool ($a,$b,$c --> String)

It's worth noting if you do not define parameters with the read-write trait explicitly (is rw attached to the particular parameter), they will be readonly aliases, i.e. immutable in the context of your sub. Also, if your sub declaration says it takes no parameters, but ones are passed anyway, they are passed in @_:

And so on and so on. Now, let's continue with some more interesting parts of parameters in Perl 6.

Optional parametersGenerally you will pass your arguments in order to the function; after these ordered, required arguments you may specify several other types of parameters, optional parameters are one of these. Optional parameters simply appear after all required arguments and are post-fixed with the ? symbol. Example:

Named ParametersYou can also explicitly associate your parameters with a name, by prefixing it with :. This forces the parameter you mark in question to be passed with a name, rather than being passed by position. Example:

Multi-subs and signaturesIn Perl 6, the parameters an argument accepts defines what is called the subroutine signature. Signature's are your function differentiator when you call a sub that is defined twice, over two different sets of parameters. This difference is based on the type annotations you provide in the signature. Multi-subs allow you to basically overload functions in the classic sense; you use the multi keyword on a sub to state it will be defined more than once with different signatures, and when you call it, the appropriate sub will be called based on signatures.

CurryingCurrying a function allows you to take a function with multiple parameters and reduce it to a function with one parameter (essentially.) This allows you to curry multi-argument subs and use them in operations like map, where the function is expected to take one argument. Example:

In this case, $c would be a function that simply took numbers to the power of 2.Currying is pretty useful as it allows us to define a sub that may take multiple parameters, but map it over a list of values sequentially as if it only took one, i.e.:

Wrapped SubroutinesAside from currying, it is possible to have fun with subroutines in another manner: by defining a function wrapper that is executed when sub is called. Wrappers basically allow you to write fairly polymorphic/generic functions, and wrap them with a specific functionality using full Perl 6. It's important to note though, that this wrap actually modifies the function itself; it doesn't return you an anonymous function in a Scalar or somesuch (we'll get to this in a sec.)

All Routine's have a .wrap method. It expects one argument of type Code (a block of code, essentially.) This parameter may have the special expressions callsame,callwith, nextsame and nextwith within it. These all call the original Routine, but you may specify different parameters, etc..Example:

The id returned by a .wrap method can later be used to .unwrap the method. A call to .wrap replaces the previous routine definition with what you supply as your Code block, and you may have multiple wraps. Therefore, it's wise to get the return value of .wrap so you may unwrap it later:

(addendum: so far, .wrap doesn't work like above on pugs r16525; ergo, the above is fabricated, but accurate. It is however documented in S06 and will most likely be implemented sometime in the near future [hopefully.])

ConclusionWell, in this article I roughly covered subroutines in Perl 6. There're quite a few changes, so you need to adjust to them; none are too daring to accept, however. Next round, I think we'll cover Object-orientation in Perl 6, before moving to Grammars and rules.

Tuesday, May 22, 2007

Note: Oh, before we get started, a quick word I probably should have addressed in my last post: it'd be good to note by anybody reading this blog, that the face of Perl 6 can change pretty quickly. Code may work one day and not the next. It is therefore important not to focus entirely on the syntax of the code: the scales tilt better in favor of semantics than syntax. But just know, semantics are subject to change as well (although I hope to get the general point across.)It will, therefore, not be my responsibility to update the code to reflect future changes to Perl 6 and Pugs. What you read here should be taken with a grain of salt and it may not work out of the box for you. For this reason, I'm going to start posting the revision number of my Pugs version at the top of my blogs (such as seen starting with this post; note that if I decide to deal with the v6.pm Perl5 module, I will state so explicitly), so you can get an idea of the 'when and where this worked.' This isn't a solid metric; it is merely suggestive.Essentially, be warned: frustration may lie within.

So now that such a formality is out of the way (albeit, an important one,) let's go!

Starting offI'm sure you're as ready as I am to get hacking with Perl 6. Let's start by doing some simple evaluations in Pugs:

The pugs prompt essentially is to Perl 6 as GHCi is to Haskell: Expressions are evaluated in the prompt and pugs will give you the overall result of your expression. Any arbitrary expression may be entered into the prompt providing it is syntactically and/or semantically correct.

An interesting point to note here is the absence of a semicolon in our statements: an expression sans a semicolon in the pugs prompt will evaluate the expression, and give us the overall value of the expression, i.e. the return value, with or without side effects (such as I/O.) A semicolon in the above statements would result in nothing being outputted: the return value is computed, but not printed to standard output.This is a good point to know; absence of a semicolon in your expressions will give you the result of the expression as far as pugs itself is concerned, i.e. it may tell you True, undef, or it may even return the 'de-sugared' form of an object. A semicolon at the end essentially just 'mutes' your output a little (as far as this introductory tutorial goes.)

Also note that types in Perl 6 are strictly optional. Like it's predecessors, Perl 6 is dynamically typed; however, a type system does exist now: type annotations to specify what a variable is/holds are strictly optional, however.In perl 6, there are two distinct different types: Mutable types, and Immutable types (Note: these definitions are fairly lax. For a stricter definition, you should refer to S02.) Mutable types are essentially the type a variable actually is, as opposed to what it contains. These types include Scalar, Array, Hash, Sub, etc. etc.. Immutable types are are the other half: the type a variable may contain. These are types such as Str, Int, Complex, and Bool.

Now, we'll continue. It's only proper to do a 'hello world' in a language when you first start it. Let's do that now:

pugs> say "hello world";hello worldpugs>

On a side note, one of the newer things about Perl 6 is that many of the builtin functions act a lot like method's of a class; the above example could have been written this way as well:

pugs> "hello world".say;hello worldpugs>

This will work with just about any builtin function (see: S29.) The syntactical choice is merely personal preference.

At the point when writing anything really significant, we should probably get out of Pugs itself. We can simply write the code in a source file and execute like so:

We will continue to use Pugs in the prompt until we are to the point that we would rather have a script.

Variables in general

In Perl 6, you stand beside your tried and true variable types. You've still got your scalar & co.. There is a difference in Perl 6 though that needs to be addressed now, this difference being sigil invariance. Sigil invariance in Perl 6 means, that while your variable sigil's changed depending on context in Perl 5, they do not in Perl 6. You will always refer to @arr as @arr, i.e.

my @arr = (1,2,3,4,5);say @arr[1]; #this will output 2

This goes for all the basic variables in Perl 6. (Given, you can create a scalar that's a reference to an array, thus getting the same effect.)

Also, you must always explicitly define your variables in Perl 6 using 'my.' To go without it will produce an error.(edit 05/25/07: the above is not entirely correct; in perl 6, a 'use strict;' declaration is implied in code and is the default; thanks to Aankhen`` for pointing this out.)

ScalarA scalar is a variable in the most basic sense. It holds a single value of whatever you want. For example, an integer, a string, a reference, etc. etc..

To be completely honest, there isn't much to say here. So I'll just move forward.

ArrayAn array is simply a list of Scalar values, to put it bluntly. Array's can hold an arbitrary number of elements of any arbitrary type.

Declaring an array is as simple as a comma delimited list of elements, as such:

@a = (1,2,3,4,5);@b = ('a','b','c','d','e');

They can also be declared 'word style' using the < > operators:

@a = <1 2 3 4 5>;@b = <a b c d e>;

These operators in this context act much like the perl 5 construct qw(). They create an array based on the string, token delimited by any amount of whitespace (you may also use these with hashes as we'll see later.) Note that using this style, your array value's will in terms of type, be strings. However, Perl is dynamic, so s'all good baby.

It's important to know these functions cause, what's known in the Haskell world, as a destructive update. The actual array is modified your function (a side effect); you must be aware of this.

HashesA hash is essentially the same as it was in Perl 5. A hash is an array of key => value pairs. Rather than using a numerical index into a hash, you can instead use a textual index. Let's see an example:

Hash elements are referenced using a key, this key can be passed the traditional way via the { }brackets with the quoted string/variable, or you may use the < > brackets now to achieve the same effect, i.e.:

Pretty basic, as you can see. You also have an unless statement, which essentially is just the opposite of if. There are also a couple of (slightly) different control structures; namely for, loop, and given.

For is basically the equivilant to an array iterator control structure (commonly known as foreach in other cultures), example:

Loop is basically the canonical for in other languages. It is called with the traditional syntax of loop ($i=0;$i<10;$i++) you would generally see in other languages.

However, with no arguments, loop can also be used as a simple infinate loop. Example:

[altair@stormwind diveintoperl6]$ cat > loop.p6loop {say "i'm never going to terminate, ever!";}[altair@stormwind diveintoperl6]$ pugs loop.p6i'm never going to terminate, ever!i'm never going to terminate, ever!i'm never going to terminate, ever!i'm never going to terminate, ever!i'm never going to terminate, ever!i'm never going to terminate, ever!i'm never going to terminate, ever!i'm never going to terminate, ever!i'm never going to terminate, ever!i'm never going to terminate, ever!...^C goingpugs: interrupted[altair@stormwind diveintoperl6]$

Loops also have means of breaking out of loops, the same way you would use continue; or break; in other languages. These means are the keywords next,redo and last. next and last are essentially the same as continue or break (continue exists in perl 6 too, we'll get to that in a moment.) redo is a little different, essentially it is the same as next, only no counters are iterated and the loop simply starts over in the same state.

In this instance, redo essentially does the exact same as next; it is important to note that in this case, our differentiator ($i) is being modified inside the context of loop, so therefore -- as far as this example goes -- redo doesn't really accomplish much. redo is useful however when for example, you want a condition to be held and not continue to the next iteration without that condition being true (for example, you need a sub that is called in your loop to return True in order to continue with the next iteration of the loop.) This condition would most likely be dependent on some sort of a side effect (such as say an external file modification, or even another thread.) Here's a basic example:

It's important to note that, all of the when cases inside of a given have an implicit break on the end. You may fall through to the next structure however, using an explicit continue in your particular when case.

given can be used in a little more advanced manner (notably with rules,) but we'll probably move onto that in later articles.

Conclusion

This article is the first part in a few series I'll be making to introduce people to the language. At the end of these 'Rounds,' we'll get to actually writing production code and a few programs in Perl 6.

Monday, May 21, 2007

Since this is an introductory post to this, a blog dedicated to Perl 6, I figure I should establish a few things about what Perl 6 is, before just 'jumping in.' Perl 6 is more than a language, it's a community and a development effort by those willing to be involved.

To start, I'll go ahead and list of a few things from the 'Perl 6 jargon file', so you're not caught by suprise when these are referenced to later:

Apocalpyse: Larry wall, BDFL of the Perl project, designed Perl 6 to be 'the community's rewrite of perl,' where Perl 5 was his rewrite of Perl. To do this, people from all around the globe submit RFC's to the perl6-language mailing list (short form: p6l which is how I'll refer to the list from now on,) talking about what they believe what changes should take place in the language. Wall manually answers these RFCs and describes whether he accepted or rejected the change, and why. An apocalypse is a collection of these RFC's and their answers. Every apocalypse directly relates to a chapter of the Camel book.

Note, that Apocalypses, after their initial release, can be revised. It is not uncommon for an Apocalypse to soon change after it's publication; if you have code that isn't working, but only 'proved to be correct, yet not tried,' [sic] check the accompanying Apocalypse: the syntax and specifications may very well have changed.

Synopsis: After an Apocalypse, a Synopsis is written. The synopsis essentially sums up the changes taking place described in a single Apocalypse. The name is just what it sounds like. Synopsis are generally more approachable and less lengthy than their accompanying Apocalypse.

Exegesis: The Exegesis are practical explanations and code samples of the changes taking place as described by both the Synopsis and Apocalypse. These are your examples of the specifications the Apocalypses describe.

Pugs: Pugs is an implementation of Perl 6, written in Haskell. Pugs intends to bootstrap the perl 6 compiler by writing it in Haskell. The objective is that once all the pugs milestones are done, that if needed, Pugs will be rewritten in Perl 6, to develop a fully fledged Perl 6 compiler, in Perl 6. You can find more on the wiki entry. Commit access to pugs is handed out quite liberally, and anybody is free to work on whatever they feel needs to be worked on. Just the way Perl 6 is a community effort and a rapidly changing landscape, so is Pugs.

Note that Pugs is not the only implementation of Perl 6, it is merely the most complete and established. Via the use of the v6.pm module, it's possible to write limited amounts of Perl 6 in Perl 5 (you may think it as either 'perl-on-perl' action or 'perl-on-haskell' action.)In my future posts, I will generally stick to Pugs for the implementation, as it is more feature complete and more accurately reflects the current state of Perl 6.

Parrot: Parrot is a virtual machine, designed to run Perl 6. The virtual machine is designed for fast (one of it's primary goals) execution of dynamic languages such as, but not limited to, Perl 6. Multiple languages can run on Parrot (languages for parrot can be constructed via the use of a very interesting compiler-toolchain, designed for Parrot), and the architecture is modern and well done. Parrot is discussed primarily on the perl6-internals mailing list (p6i.)

Please keep in mind, development of Parrot in relation to the actual Perl 6 language is nigh orthogonal: Parrot's only obligation is to support Perl 6 and it's semantics, but the language designers have no say in how it's done or why. Parrot is, as well, the only virtual machine for Perl 6. Communication between these two branches is done, however, they are two seperate entities by all practical means.

So now that the lingo is out of the way, what's the real deal with Perl 6? How can you get involved?Getting involved is essentially as simple as joining the mailing lists (and I would recommend joining IRC as well: irc.freenode.org #perl6) and posting. That's it. Perl 6 is a community effort in its entirety: you, the people, say what stays and what goes. Submit your RFCs, comment on others, the power to change the language is yours.

Aside from the community, Perl 6 has changed a lot technically. Niklaus Wirth said that if we add things to a language, we must throw things away. You are still writing perl, and you will still be backwards compatible, but a lot of things are changing from Perl 5 to Perl 6. Things are getting added, things are getting thrown away.

Quick examples are:

Sigil invariance. When referencing an array itself (or any other type, for that matter), or an element of the array, you always give it the original sigil you declared it with, i.e. you are going from $arr[1]; to @arr[1];. Note, context in Perl 6 still exists, but sigils do not vary.Example:

Object orientation is full blown now. Classes and object-orientation is greatly improved, as you now have classes, [multiple] inheritence, private/public pieces of data, constructors and destructors; object orientation is much more complete.

Regex's no longer exist. You're using Rules, now (this change in lexicon is due to the fact that perl's "regular expressions" are so far off the mark of a traditional, formal regular expression, that they were renamed.) One of the greater things about Rules and Grammar's in the new Perl 6 engine is that Grammars are essentially 'classes for rules,' a grammar just inherits from the Rule class itself.

Static types. Perl 5 was a dynamic language, Perl 6 adds static typing capabilities. These are simply strictly optional annotations to your already existing variables to explicitly define your type. Either style of programming can be used.It's interesting to note that as you can specify types to a variable, you can specify two kinds: a type refering to what the variable holds (strings, int's, etc.) and a type referring to what the variable actually is; a hash, an array, ex:

pugs> my Int $i is scalar;pugs> $i = 1;pugs> say $i1Bool::Truepugs>

This is not limited to the built in types, any may do, including classes.

Laziness. Lists and the like are now implicitly lazy, meaning you can do things such as my @a = 1...;

Various syntax changes. Parenthesis in a large majority of constructs are strictly optional. Array's may be constructed in a 'stream of words' fashion using the < > operators, rather than qw(). I.e., from my @a = qw(a b c d); you go to my @a = <a b c d>;. Hash values can be referenced the same way, i.e. %hash<some_key>; Prototypes for subs can simply be done via sub name { ... };. There are more various little changes around, but none of these are hugely significant ones.

Those are just a few of the changes. For more, I suggest you read these.

Perl 6 is a big change to an already very popular programming language. One isn't going out the door for another to come in the same day: the adoption will be slow, but it is a needed change and a good one for both the Perl community, and Perl itself. If you would like to contribute, I can only suggest you sign onto IRC/mailing lists, and you speak your voice and do what you think you need, or are able, to do.