Perl6-Pugs/docs/Perl6/Perl5/Differences.pod
=head1 NAME
Perl6::Perl5::Differences -- Differences between Perl 5 and Perl 6
=head1 DESCRIPTION
This document is intended to be used by Perl 5 programmers who are new to Perl
6 and just want a quick overview of the main differences. More detail on
everything can be found in the language reference, which have been linked to
throughout.
This list is currently known to be incomplete.
=cut
# S02
=head1 Bits and Pieces
=head2 Sigils
Where you used to say:
my @fruits = ("apple", "pear", "banana");
print $fruit[0], "\n";
You would now say:
my @fruits = ("apple", "pear", "banana");
say @fruit[0];
Or even use the C<< <> >> operator, which replaces C<qw()>:
my @fruits = <apple pear banana>;
Note that the sigil for fetching a single element has changed from C<$>
to C<@>; perhaps a better way to think of it is that the sigil of a
variable is now a part of its name, so it never changes in subscripting.
The same applies to hashes:
say "There are %days{'February'} days in February";
Again, there is a shorter form:
say "There are %days<February> days in February";
For details, see L<S02/"Names and Variables">.
=head2 Global variables have a twigil
Yes, a twigil. It's the second character in the variable name. For globals,
it's a C<*>; for contextual variables, it's C<+>.
Was: $ENV{FOO}
Now: %*ENV<FOO>
Or: $+FOO # might be overridden, but falls back to %*ENV
For details, see L<S02/"Names and Variables">.
=head2 New ways of referring to array and hash elements
Number of elements in an array:
Was: $#array+1 or scalar(@array)
Now: @array.elems
Index of last element in an array:
Was: $#array
Now: @array.end
Therefore, last element in an array:
Was: $array[$#array]
Now: @array[@array.end]
@array[-1] # also works
For details, see L<S02/"Built-In Data Types">
=head2 The double-underscore keywords are gone
Old New
--- ---
__LINE__ $?LINE
__FILE__ $?FILE
__PACKAGE__ $?PACKAGE
__END__ =begin END
__DATA__ =begin DATA
See L<S02/"double-underscore forms are going away"> for details.
=cut
# S03
=head1 Operators
A comprehensive list of operator changes is documented at L<S03/"Changes to
Perl 5 operators"> and L<S03/"New operators">.
Some highlights:
=head2 C<qw()> has a customary form; new interpolating form
Was: qw(foo)
Now: <foo>
Was: ("foo", (split /s+/, $bar), "bat")
Now: <<foo $bar bat>>
Quoting operators now have modifiers that can be used with them (much like
regexes and substitutions in Perl 5), and you can even define your own quoting
operators. See L<S03> for details.
=cut
# S04
=head1 Blocks and Statements
See L<S04> for the full specification of blocks and statements in Perl6.
=head2 You don't need parens on control structure conditions
Was: if ($a < $b) { ... }
Now: if $a < $b { ... }
Likewise for C<while>, C<for>, etc.
=head2 eval {} is now try {}
Using C<eval> on a block is now replaced with C<try>.
Was: eval {
# ...
};
if ($@) {
warn "oops: $@";
}
Now: try {
# ...
CATCH { warn "oops: $!" }
}
CATCH provides more flexiblity in handling errors.
See L<S04/"Exception_handlers"> for details.
=head2 foreach becomes for
Was: foreach (@whatever) { ... }
Now: for @whatever { ... }
Also, the way of assigning to something other than C<$_> has changed:
Was: foreach my $x (@whatever) { ... }
Now: for @whatever -> $x { ... }
This can be extended to take more than one element at a time:
Was: while (my($age, $sex, $location) = splice @whatever, 0, 3) { ... }
Now: for @whatever -> $age, $sex, $location { ... }
(Except the C<for> version does not destroy the array.)
See L<S04/"The for statement"> and L<S29/"each"> for details.
=head2 for becomes loop
Was: for ($i=0; $i<10; $i++) { ... }
Now: loop ($i=0; $i<10; $i++) { ... }
C<loop> can also be used for infinite loops:
Was: while (1) { ... }
Now: loop { ... }
=cut
# S05
=head1 Regexes and Rules
Here's a simple translation of a Perl5 regular expression to Perl6:
Was: $str =~ m/^\d{2,5}\s/i
Now: $str ~~ m:P5:i/^\d{2,5}\s/
The C<:P5> modifier is there because the standard Perl6 syntax is rather
different, and 'P5' notes a Perl5 compatibility syntax. For a substitution:
Was: $str =~ s/(a)/$1/e;
Now: $str ~~ s:P5/(a)/{$0}/;
Notice that C<$1> starts at C<$0> now, and C</e> is gone
in favor of the embedded closure notation.
For the full specification, see L<S05>. See also:
The related Apocalypse, which justifies the changes:
http://dev.perl.org/perl6/doc/design/apo/A05.html
And the related Exegesis, which explains it more detail:
http://dev.perl.org/perl6/doc/design/exe/E05.html
=cut
# S06
=head1 Subroutines
=cut
# S07
=head1 Formats
=cut
=head2 Formats have been replaced with forms.
For details see :
L<http://dev.perl.org/perl6/doc/design/apo/A07.html> Format Apocalypse
L<http://dev.perl.org/perl6/doc/design/exe/E07.html> Format Exegesis
=cut
#S10
=head1 Packages
=cut
#S11
=head1 Modules
=cut
#S12
=head1 Objects
=head2 Method invocation changes from -> to .
Was: $object->method
Now: $object.method
=head Using code references in dynamic method calls is gone.
Was: $self.$coderef()
Now: $coderef(self:)
=cut
#S13
=head1 Overloading
=cut
#S16
=head1
=head2 Chaining file test operators has changed
Was: if (-r $file && -x _) {...}
Now: if -r -x $file {...}
For details, see L<S03/"Changes to Perl 5 operators"/"The filetest operators now return a result that is both a boolean">
=cut
#S26
=head1 Documentation
You now use <em>kwid</em> for documentation instead of POD. Kwid
is a wiki-like syntax that is easy to write, and pleasant to read directly.
Here is a side-by-side comparison of some of the major features of Pod and
Kwid:
=head1 Big Thing = Big Thing
=head4 Small Thing ==== Small Thing
A paragraph of A paragraph of
plain text. plain text.
# verbatim # verbatim
sub v { sub v {
shift; shift;
} }
=item * foo * foo
=item * bar * bar
=item2 N<> barber ++ barber
=item2 N<> bard ++ bard
Something B<strong>! Something *strong*!
Something I<emphatic>! Something /emphatic/!
Some code C<E = M * C ^ 2>! Some code `E = M * C ^ 2`!
Some V<B<escaped>> markup Some \*escaped\* markup
=begin Section_type .Section_type
=end Section_type !Section_type
=for Section_type :Section_type
See L<S26> for details.
=cut
#S29
=head1 Builtin Functions
A number of builtins have been removed. For details, see:
L<S29/"Obsolete">
=head2 References are gone (or: everything is a reference)
C<Capture> objects fill the ecological niche of references in Perl 6.
You can think of them as "fat" references, that is, references that
can capture not only the current identity of a single object, but
also the relative identities of several related objects. Conversely,
you can think of Perl 5 references as a degenerate form of C<Capture>
when you want to refer only to a single item.
Was: ref $foo eq 'HASH'
Now: $foo ~~ Hash
Was: @new = (ref $old eq 'ARRAY' ) ? @$old : ($old);
Now: @new = @$old;
Was: %h = ( k => \@a );
Now: %h = ( k => @a );
To pass an argument to modify by reference:
Was: sub foo {...}; foo(\$bar)
Now: sub foo ($bar is rw); foo($bar)
The "obsolete" reference above has the details. Also, look for
I<Capture> under L<S02/"Names_and_Variables">, or at the Capture FAQ,
L<Perl6::FAQ::Capture>.
=head2 say()
This is a version of C<print> that auto-appends a newline:
Was: print "Hello, world!\n";
Now: say "Hello, world!";
Since you want to do that so often anyway, it seemed like a handy thing
to make part of the language.
=head1 Unfiled
=head2 Hash elements no longer auto-quote
Hash elements no longer auto-quote:
Was: $days{February}
Now: %days{'February'}
Or: %days{"February"}
Or: %days<February>
Or: %days<<February>>
The curly-bracket forms still work, but curly-brackets are more
distinctly block-related now, so in fact what you've got there is a
block that returns the value "February". The C<<>> and C<<<>>> forms
are in fact just quoting mechanisms (see below).
=head2 Built-in functions are now methods
Most (all?) built-in functions are now methods of built-in classes such
as C<String>, C<Array>, etc.
Was: my $len = length($string);
Now: my $len = $string.chars;
Was: print sort(@array);
Now: print @array.sort;
@array.sort.print;
You can still say C<sort(@array)> if you prefer the non-OO idiom.
=head1 AUTHORS
Kirrily "Skud" Robert, <skud@cpan.org>,
Mark Stosberg,
Trey Harris