Learning Perl 6
Okay, let's see what we can do to get the basics of Perl 6 down.
The Shell
pugs> say "Hello" Hello bool::true
Notice that you don't need to use semi-colons in
the shell. Pugs is pretty generous in that regard. Perl 6
uses say to print out a line of text. This
is a bit easier than earlier versions, which forced you
to append a newline at the end of all your
print statements. That
bool::true bit is the return value
of the expression, which we usually don't worry about
with functions like say.
So now we know how to print a line of text. Okay, now let's get some user input.
pugs> my $name = =$IN Brian "Brian\n"
This is a pretty simple example of declaration and
assignment. Perl users should already be familiar with
my@, which declares a new lexical variable. The
@$name tells Perl it's a scalar variable
named, well, name. So far so good.
But what's that on the other side of the equal
sign? It's certainly not how we're used to
getting our user input. This is the new Perl 6 way. Hey,
I'll be honest. I don't really understand
what's going on with the extra dollar sign, but I do
get that $IN is the (probably) global
variable representing STDIN. Until we get a
clue, we'll just follow this practice blindly, and
use =$IN whenever we need to get something
from the standard input stream.
I enter my name, and the Pugs shell shows us the
return value that gets assigned to $name:
"Brian\n". There's that
newline at the end of all input. Well, this is Perl after
all, so we still need to chomp our input so
we can make good use of it in our own code.
pugs> $name = chomp($name) "Brian"
chomp makes a little more sense to me now
than it did in earlier versions of Perl. Instead of
directly altering the string and returning the number of
characters "chomped" from the end, it returns
an altered copy of the string. It's a small thing,
but it shows a big shift in how Perl 6 works. Less
hackery magic, more straightforward coding.
After all these little changes, I need to see something that works the same.
pugs> say "Hello $name" "Hello Brian" bool::true
String interpolation seems to work the same as it
always has, at least for simple scalars like
$name. This is a pleasant relief, even if I
do lean towards concatenating rather than interpolating.
But concatenation has changed as well:
pugs> say "Hello " ~ $name "Hello Brian" bool::true
So interpolate or concatenate, whichever.
Now that we've played a little bit in the Pugs shell, handling input and output from the Perl 6 perspective, let's put it all into a script.
Writing a Quick Script
Let's write a script version of what we did, with a little addition.
print "What's your name? "; my $name = =$IN; $name = chomp($name); say "Hello, $name!";
Since I'm still doing a lot of Perl 5 work, I
chose to save this file as hello.p6. I lose
my precious syntax highlighting, but I can live without
that for now.
First thing you'll notice is that we're back to using semi-colons; We may not need them inside the shell, but the interpreter still needs them to seperate statements while handling a file.
I also used the good old print function
to display some text without a newline at the end. We
have say to save us from using
print a lot of times, but occasionally
print still makes more sense.
Running this script is very easy, assuming that pugs is on your path.
C:\projects>pugs hello.p6 What is your name? Brian Hello, Brian!
And that's our quick glance at some of the small differences between Perl 5 and Perl 6. I'll look at more when I get the opportunity.
