CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Programming] Perl6 FAQ


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author Bastian On 2007-04-29 10:02:22




View and vote on the article here: Perl6 FAQ


Perl6 FAQ

Category
Programming
Summary
I'm sure a lot of Perl programmers have asked themselves the same questions:
"Why have they developed Perl6?"
"Is it really necessary?"
"Will I have to rewrite all my Perl5 Stuff?"
"Will Perl5 still be maintained?
Body
I'm sure a lot of Perl programmers have asked themselves the same questions:
"Why have they developed Perl6?"
"Is it really necessary?"
"Will I have to rewrite all my Perl5 Stuff?"
"Will Perl5 still be maintained?

Let's answer these questions:

"Why they developed Perl6?"
- Major changes are most unlikely due to the hacked nature of the Perl5 internals.
- The Perl5 implementation is too complex.
- Perl6 will incorporate improvements to the language and to the internal API.

"Who develops it?"
Perl6 will be a complete rewrite of Perl by the Perl community, which means everyone can contribute.

"Will I have to rewrite all my Perl5 stuff?"
Definitely not! The PONIE [Perl On New Internal Engine] Project will be a bridge between Perl5 and Perl6. The Perl5 interpreter will be able to run on the Perl6 virtual machine.
Furthermore Perl6 will have a "Perl5 compatibilty mode". The compiler will execute any code that it recognizes as being written in Perl5.
[Resources: http://www.poniecode.org]

"What shall I do with the mass of modules I wrote in Perl5 and shared with CPAN?"
You can run your modules on Perl6 via PONIE.

"Will Perl6 have the same compiler Perl5 is currently using?"
No. In Perl6 the language syntax and implementation internals have been separted (like the Jave Runtime by Sun). The language-independent runtime device is called Parrot.

"Is Parrot only able to run Perl code?"
No, other languages can be implemented, such as PHP, Python, Java or Ruby. You can add other languages to Parrot, too. You just have to write the PASM (Parrot Assembler) or PIR (Parrot Intermediate Representation) interface.

"When will Perl6 be published?"
Due to certain funding problems the date for the development and production release have been postponed. At the moment, the time for the development release is Quarter 1 2006 and Quarter 1 2007 for the production release.

"What are the differences between Perl5 and Perl6 affecting me while I'm coding?"

::Hello World::
The Hello World Program in Perl6:
say "Hello World"; 
# say is like print with an appended newline
# at the end of the output (like writeln in Pascal)
::Variables::
Perl5 has a dynamic type system, whereas Perl 6 uses static types.
For example:
#Perl5 code
my $a = 2;
my $b = 3.14;
my $c = "Hello, my name is n00b";

--

#Perl6 code
my int $a = 2;
my num $b = 3.14;
my str $c = "Hello, my name is n00b";
Perl6 has the following built-in scalar types.

bool - a boolean (true or false) value
bit - a single bit, with the value 1 or 0
int - an integer
num - a floating point number
str - a string
ref - a reference



In case you forget the scalar type Perl6 tries to find out what type it is.
For example:
my $var = 3.14; # 3.14 is assumed to be a number
::Sigil Invariance::
In Perl5, sigils (the punctuation characters that precede a variable name) change depending on how the variable is used:
#Perl5 code
my @array = (0,1,2,3,4);
my $element = $array[3];

--

#Perl6 code
my @array = (0,1,2,3,4);
my $element = @array[1];
::Subroutines::
A Perl5 subroutine looks like this:
#Perl5 code
sub execute_something($a,$b,$c) {...}
A Perl6 subroutine looks like this:
#Perl6 code	
sub execute_something(int $a, num $b, str $c) {...}
The formal parameters are aliases to the actual paramaters. By default they are marked constant. This means they can't be modified.
sub incr(num $x) {
   $x++; #executing this subroutine will cause a compile-time error
}
However, the parameters can be modified with "is copy" and "is rw" [read-write].
In case of "is copy" Perl6 copies the actual parameter's data than aliasing it.
In case of "is rw" the alias isn't marked constant. [
 sub incr(num $x is rw) { $x++ } 
]

Furtheremore there are a number of features in parameter lists which make parameter passing much more powerful than in Perl5:

* = after the parameter can be used to assign default values
* ? before the parameter indicates optional arguments
* + before the parameter indicates a named argument (passed like an element of a hash)
* where can supply a condition that the actual parameter must match

For example:
sub my_split(Rule ?$pat = rx/\s+/, Str ?$expr = $_, Int ?$lim where $^li)
::Regular Expressions::
The term Regular Expression doesn't exist anymore in Perl6. It has been renamed to "Rules".
Only five features survived the change from Perl5 to Perl6:
- Capturing: (...)
- Alternatives: |
- Backslash escape: \
- Repetition quantifiers: *, + and ?
- Minimal matching suffix: *?, +?, 
Here are some of the most important new features:
- Simplified non-capturing groups: [...], which are the same as (?:...) in Perl5
- Commit at various levels of scope via: :, ::, :::, and <commit>
- Simplified code assertions: <{...}>
- Perl5's /x is now the default
- define grammars that encapsulate related rules

::Syntactic Simplification::
The round brackets required in control flow constructs in Perl5 are now optional.
if is_true() {
        for @array {
             ...
        }
}
The three dots above are new in Perl6. They are called the "yadda-yadda operator". You can use them as placeholders for instance. However, if the program attempts to execute "..." it raises an exception.


::Chained Comparisons::
if 150 <= $inta == $intb <= 200 { say "Double check!" }
In Perl6 this code works as expected.

::Lazy evaluation::
my int @array = 0..Inf; #integeres from 0 to infinity
 for @array - $counter {
          say "$counter";
          last if $counter >= 10;
}
In Perl6 the code above works fine, too. It won't crash because of the Inf term.
It will show you the numbers from 0 to 10 as requested.

You can find some more laziness if you have a look at the new style for reading input:
#Perl5 code
while (<>) {
   print;
}

--

#Perl6 code
for =<> {
   print;
}
::Junctions::
In principle you create junctions by connecting values with junctive operators:
my $even_digit = 0|2|4|6|8; # any (0,2,4,6,8)
my $odd_digits = 1&3&5&7;   # all (1,3,5,7)
my $not_zero = none(0);
These values can be used arithmetically;
my $junction = 1|2|3;
$junction += 4;     #junction now equals 4|6|7
$junction += (1&2); #junction now equals (6|7|8)&(7|8|9)
Or in comparisons:
if $grade eq any('A'..'D') { say "pass"}
Or in subscripting:
if %person{any('first_name','nickname')} eq "Joe" { say "How do you do, Joe?"}
Junctions aren't ordered. That means 1|2|3 is the same as 3|2|1. Due to this lack of ordering the Perl6 interpreter can choose to evaluate junctive expressions in parallel. Quite a lot of people believe that junctions could supercede explicit multithreading.
For instance:
for all(@array) {...}
This code would tell the compiler that the loop should be run parallel, not serial.

::Object Oriented Perl6::
To create an object of a particular class in Perl5 you've to use the following syntax:
#Perl5 code
my $obj = bless $reference, 'Class';
With the "arrows" you can execute a method using the object.
For instance:
#Perl5 code
$obj-method();
The blessing model is still available in Perl6. However, the developers created a new model to create classes/objects.
For instance, a class to encapsulate a Cartesian point could be written:
#Perl6 code
class Point is rw {
    has $.x;
    has $.y;
}
And then use:
#Perl6 code
my Point $point .= new;
$point.x = 1.5;
$point.y = -3.14;
As you can see the dot replaces the arrow, as in Java, Ruby or Python.

Those were the most important changes between Perl5 and Perl6.

"Is Perl5 ready for the trash can now?"
No, of course not. Perl5 maintenance continues and won't be discontinued any time soon. New releases are planned for the next several years. Furthermore the Perl5 code will run on Parrot via PONIE so no-one will be force to update their Perl version.

So it's up to you what to use in the future. Make up your own mind about it. I think both will be fun.


This article was imported from zZine. (original author: Bastian)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.01630 seconds


About Us | Privacy Policy | Mission Statement | Help