While Perl 6 should be able to attract developers from other languages as well, it seems it will be especially interesting for people with some background in Perl 5. Therefore, it can be useful to have a set of articles comparing Perl 5 and Perl 6.
In this article, we are going to look at the scalars variables, and some of the functions dealing with them.
In Perl 5 you normally use print(), or starting from 5.10 you can use say() to print to the screen. The latter will append a newline to whatever you had to say.
In Perl 6 they work the same.
use v6; print "Hello World\n"; say "Hello World";
In Perl 6 you almost always need to declare your variables with my. Think as if you had always added use strict. Just as it is done by use 5.012;
There are exceptions, such as one-liners and certain constructs that auto-declare the scoped variables for you.
use v6; my $name = "Moose";
In Perl 6 scalar variables still interpolate in double quoted strings:
use v6; my $name = "Foobar"; say "Hello $name"; # Hello Foobar
In order to make the Object Oriented people happy Perl 6 also allows calling the say method on scalar variables:
use v6; my $name = "Foobar"; "Hello $name".say; # Hello Foobar
In Perl 5 you would use <STDIN>.
As reading a single line from standard input is usually preceded by a print statement, the keyword to do that in Perl 6 is called prompt().
use v6; my $line = prompt('Please type in your name: '); say "Hi $line, how are you?"; # Hi Foobar, how are you?
If you try this you will see that it chomps the newline off automatically. Repeated reading without a prompt will be explained later.
In Perl 6 the file handle holding the connection to standard input is kept in the $*IN variable. It can be used together with the get method to read a single line from the standard input:
use v6; print "What's your name? "; my $line = $*IN.get; say "Hi $line, how are you?"; # Hi Foobar, how are you?
The substr() function of Perl 6 is the similar to what we had in Perl 5, except that there is no 4 parameter version. As all the other functions it can also work in the object oriented way.
The parameters that used to accept negative values, now require, that you precede the negative value with *, the whatever operator.
use v6; my $line = "The brown cat climbed the green tree"; say substr $line, 4, 3; # bro $line.substr(4, 3).say; # bro say substr $line, 4, *-27; # brown
In Perl 6 there is no length() function. Instead, there is a function called chars(), that will return the number of characters.
use v6; my $a = "This is a string"; say chars $a; # 16
In Perl 5 there is a chomp() function to remove a trailing newline after reading a line from the standard input or from a file.
In Perl 6 this function will be almost never used as reading from the standard input or from files will automatically chomp off the newlines. In any case, in Perl 6 chomp behaves differently as it returns the chomped string and does NOT change the original string.
use v6; my $a = "abcd\n"; my $b = chomp $a; print "<$b>"; # $b is now "abcd" print "<$a>"; # $a still has the trailing newline
The defined() function is the same in Perl 6 as in Perl 5, checking if a scalar value has any value different from undef. Except that in Perl 6 there are several different values which fall in this category.
In Perl 5 dot . is used for string concatenation. I think it is a lot less used than one would think as in many cases we use string interpolation. Maybe one of the most frequent use-cases is actually the short-cut string concatenation.
In Perl 6 the string concatenation is done using the tilde ~ operator. IMHO it will be even less used than in Perl 5, as the variable and code interpolation in Perl 6 is much stronger. Anyway here is the example:
use v6; my $str = "Foo" ~ "Bar"; $str.say; # FooBar
I think the only place where it will be used a lot is the short-cut version that looks like this ~=
use v6; my $str = "Foo" ~ "Bar"; $str ~= " and Moo"; say $str; # FooBar and Moo
This can of course lead to some confusion, as it looks quite similar to =~, the regex operator of Perl 5, but =~ is not a valid operator in Perl 6.
If you use it by mistake, you will get a compilation error like this:
===SORRY!=== Unsupported use of =~ to do pattern matching; in Perl 6 please use ~~
String repetition that used to be the x operator stayed the same:
use v6; say "abc" x 3; # abcabcabc
The index() and rindex() functions of Perl 5, that can locate a substring, stayed the same in Perl 6 as well.
use v6; my $s = "The brown cat climbed the green tree"; say index $s, "b"; # 4 say rindex $s, "b"; # 18
lc() and uc() turning strings to all lower case and all upper case are the same in Perl 6 as in Perl 5.
use v6; say lc "Hello World"; # hello world say uc "Hello World"; # HELLO WORLD
I know these are no big tricks here, just plain code, but I hope this will help some people in learning Perl 6.
Published on 2012-08-09