Home‎ > ‎Programming/Linux‎ > ‎Perl‎ > ‎

Perl6

Perl6 is a a really cool language I've been playing with the Rakudo and Niecza implementations I've had a bit of a go at Pugs and STD/Viv as well as Yapsi
Here are some examples of features the actual source code can be found bellow in an order other than that which I expound them: 
here is a hello world program that pauses for input at the end.

#!/usr/bin/perl6
use v6;
my $msg = "Howdy";
say "$msg Cowboy";
say $*IN.get;
say $*IN.lines(1);

This will actually pause for two lines of input and write both of them out. I did this to show the two ways I've found to get input form stdin now called $*IN. the program actually says "Howdy Cowboy" rather than "Hello World" but since when have I been traditional. and here is another example: 
module Fact;
multi sub postfix:<!>(Int $n) is export {
  [*] 1..$n;
}

sub prefix:<√>(Num $n) is export {
return sqrt($n);
}

sub infix:<.oO>($name, $thought) is export {  
    say "$name thinks $thought"
}

sub infix:<+->($a, $b) is export { ($a - $b) .. ($a + $b) }

Note the parameters are typed also note I'm defining new operators including a postfix ! for factorial,  here is an example of them in use.

#!/usr/bin/env perl6-git
use v6;
use Fact;

say 0!;
say 1!;
say 6!;
say 10!;
say √ 64.Num;
say √√√256.Num;
"grizzly" .oO "Perl 6 is cool";

say 2.78  ~~ 10 +- 3 ?? 't' !! 'f';     # f
say 7.5   ~~ 10 +- 3 ?? 't' !! 'f';     # t
say 13    ~~ 10 +- 3 ?? 't' !! 'f';     # t
say 13.1  ~~ 10 +- 3 ?? 't' !! 'f';     # f

Note the cast to turn Ints into Nums I shouldn't have to do this but Rakudo cannot seam to work out that Ints and Rats are Nums I'm using the current Trunk version from github to get (git clone git://github.com/rakudo/rakudo.git), Niecza cannot handle the postfix ! it lets you define this, but when you use it it thinks a prefix not ! is being abused.

here are some more examples:
#!/usr/bin/env perl6-git
use v6;


for @*ARGS -> $arg {
    my Num  $target = $arg.Num;
    my Num  $guess  = $target;

    while (abs( $guess**2 - $target ) > 0.000000000000000005) {
        $guess += ( $target - $guess**2 ) / ( 2 * $guess );

        say $guess;
    }
    say '=' x 80;
}

This shows how you can type variables it also shows the new @*ARGS array which replaces @ARGV as ARGV  is really quite cryptic sorry C guys.  
here is cat using the Shell::Command module  from the File::Tools library,  (see the Libraries page)
#!/usr/bin/env perl6-git
use v6;
use Shell::Command;

cat(|@*ARGS);

#`«
for @*ARGS -> $arg {
    given open($arg) {
        for .lines -> $line {
            say $line;
        }
        .close;
    }
}
»

Note | is the flatten operator now bitwise or is +| for numeric and ~| for string bitwise or also note +& is numeric bitwise and and ~& for string bitwise and. 
Also note the #`« … » multi-line comment around the alternate implementation, you can use #`( … ) or substitute any number of [ or { followed by the same number of closing brackets or braces or parenthesis or use as many French quotes as you like « and athe same number of closing French quotes » so #`(((( some stuff 
on multiple 
lines ))))
is  a valid comment. 
here are some links for further reading 
ċ
Fact.pm6
(0k)
Francis Smit,
Feb 22, 2012, 3:23 AM
ċ
cat.p6
(0k)
Francis Smit,
Feb 22, 2012, 4:10 AM
ċ
fact.p6
(0k)
Francis Smit,
Feb 22, 2012, 3:23 AM
ċ
helloworld.p6
(0k)
Francis Smit,
Feb 22, 2012, 4:26 AM
ċ
squareroot.p6
(0k)
Francis Smit,
Feb 22, 2012, 4:10 AM
Comments