| 16 | | This document is intended to be used by Perl 5 programmers who are new |
| 17 | | to Perl 6 and just want a quick overview of the main differences. More |
| 18 | | detail on everything can be found in the language reference. |
| 19 | | |
| 20 | | The ordering of this document is more or less in order from basic stuff |
| 21 | | to complex stuff. To put it another way, it's been written in the order |
| 22 | | that the topics came up while the author was learning Perl 6. |
| 23 | | |
| 24 | | =head2 say() |
| 25 | | |
| 26 | | This is a version of print() that auto-appends a newline: |
| 27 | | |
| 28 | | Was: print "Hello, world!\n"; |
| 29 | | Now: say "Hello, world!"; |
| 30 | | |
| 31 | | Since you want to do that so often anyway, it seemed like a handy thing |
| 32 | | to make part of the language. |
| | 7 | This document is intended to be used by Perl 5 programmers who are new to Perl |
| | 8 | 6 and just want a quick overview of the main differences. More detail on |
| | 9 | everything can be found in the language reference, which have been linked to |
| | 10 | throughout. |
| | 11 | |
| | 12 | This list is currently known to be incomplete. |
| | 13 | |
| | 14 | =cut |
| | 15 | |
| | 16 | # S02 |
| | 17 | |
| | 18 | =head1 Bits and Pieces |
| 80 | | Hash elements no longer auto-quote: |
| 81 | | |
| 82 | | Was: $days{February} |
| 83 | | Now: %days{'February'} |
| 84 | | Or: %days{"February"} |
| 85 | | Or: %days<February> |
| 86 | | Or: %days<<February>> |
| 87 | | |
| 88 | | The curly-bracket forms still work, but curly-brackets are more |
| 89 | | distinctly block-related now, so in fact what you've got there is a |
| 90 | | block that returns the value "February". The C<<>> and C<<<>>> forms |
| 91 | | are in fact just quoting mechanisms (see below). |
| | 78 | For details, see L<S02/"Built-In Data Types"> |
| | 79 | |
| | 80 | =cut |
| | 81 | |
| | 82 | # S03 |
| | 83 | |
| | 84 | =head1 Operators |
| | 85 | |
| | 86 | A comprehensive list of operator changes are documented here: |
| | 87 | |
| | 88 | L<S03/"Changes to Perl 5 operators"> |
| | 89 | L<S03/"New operators"> |
| | 90 | |
| | 91 | Some highlights: |
| 127 | | =head2 foreach becomes for |
| 128 | | |
| 129 | | Was: foreach (@whatever) { ... } |
| 130 | | Now: for @whatever { ... } |
| 131 | | |
| 132 | | Also, the way of assigning to something other than C<$_> has changed: |
| 133 | | |
| 134 | | Was: foreach my $x (@whatever) { ... } |
| 135 | | Now: for @whatever -> $x { ... } |
| 136 | | |
| 137 | | This can be extended to take more than one element at a time: |
| 138 | | |
| 139 | | Was: while (my($age, $sex, $location) = splice @whatever, 0, 3) { ... } |
| 140 | | Now: for @whatever -> $age, $sex, $location { ... } |
| 141 | | |
| 142 | | (Only the C<for> version does not destroy the array.) |
| 143 | | |
| 144 | | =head2 for becomes loop |
| 145 | | |
| 146 | | Was: for ($i=0; $i<10; $i++) { ... } |
| 147 | | Now: loop ($i=0; $i<10; $i++) { ... } |
| 148 | | |
| 149 | | =head2 Global Variables have a twigil |
| 150 | | |
| 151 | | Yes, a twigil. It's the second character in the variable name. For globals, |
| 152 | | it's a C<*> |
| 153 | | |
| 154 | | Was: $ENV{FOO} |
| 155 | | Now: %ENV<FOO> |
| 156 | | |
| 157 | | For details, see L<S02/"Names and Variables">. |
| 158 | | |
| 159 | | =head2 eval {} is now try {} |
| | 116 | =head2 eval {} is now try {} |
| 177 | | =head1 SEE ALSO |
| 178 | | |
| 179 | | =over |
| 180 | | |
| 181 | | =item L<S03/"Changes to Perl 5 operators"> |
| 182 | | |
| 183 | | =back |
| | 134 | =head2 foreach becomes for |
| | 135 | |
| | 136 | Was: foreach (@whatever) { ... } |
| | 137 | Now: for @whatever { ... } |
| | 138 | |
| | 139 | Also, the way of assigning to something other than C<$_> has changed: |
| | 140 | |
| | 141 | Was: foreach my $x (@whatever) { ... } |
| | 142 | Now: for @whatever -> $x { ... } |
| | 143 | |
| | 144 | This can be extended to take more than one element at a time: |
| | 145 | |
| | 146 | Was: while (my($age, $sex, $location) = splice @whatever, 0, 3) { ... } |
| | 147 | Now: for @whatever -> $age, $sex, $location { ... } |
| | 148 | |
| | 149 | (Only the C<for> version does not destroy the array.) |
| | 150 | |
| | 151 | See L<S04/"The for statement"> for details. |
| | 152 | |
| | 153 | =head2 for becomes loop |
| | 154 | |
| | 155 | Was: for ($i=0; $i<10; $i++) { ... } |
| | 156 | Now: loop ($i=0; $i<10; $i++) { ... } |
| | 157 | |
| | 158 | =cut |
| | 159 | |
| | 160 | # S05 |
| | 161 | |
| | 162 | =head1 Regexes and Rules |
| | 163 | |
| | 164 | =cut |
| | 165 | |
| | 166 | # S06 |
| | 167 | |
| | 168 | =head1 Subroutines |
| | 169 | |
| | 170 | =cut |
| | 171 | |
| | 172 | # S07 |
| | 173 | |
| | 174 | =head1 Formats |
| | 175 | |
| | 176 | =cut |
| | 177 | |
| | 178 | =head2 Formats have been replaced with forms. |
| | 179 | |
| | 180 | For details see : |
| | 181 | |
| | 182 | L<http://dev.perl.org/perl6/doc/design/apo/A07.html> Format Apocalypse |
| | 183 | |
| | 184 | L<http://dev.perl.org/perl6/doc/design/exe/E07.html> Format Exegesis |
| | 185 | |
| | 186 | =cut |
| | 187 | |
| | 188 | #S10 |
| | 189 | |
| | 190 | =head1 Packages |
| | 191 | |
| | 192 | =cut |
| | 193 | |
| | 194 | #S11 |
| | 195 | |
| | 196 | =head1 Modules |
| | 197 | |
| | 198 | =cut |
| | 199 | |
| | 200 | #S12 |
| | 201 | |
| | 202 | =head1 Objects |
| | 203 | |
| | 204 | =head2 Method invocation changes from -> to . |
| | 205 | |
| | 206 | Was: $object->method |
| | 207 | Now: $object.method |
| | 208 | |
| | 209 | #S13 |
| | 210 | |
| | 211 | =cut |
| | 212 | |
| | 213 | =head1 Overloading |
| | 214 | |
| | 215 | =cut |
| | 216 | |
| | 217 | #S29 |
| | 218 | |
| | 219 | =head1 Builtin Functions |
| | 220 | |
| | 221 | A number of builtin's have been removed. For details, see: |
| | 222 | |
| | 223 | L<S29/"Obsolete"> |
| | 224 | |
| | 225 | =begin TODO |
| | 226 | |
| | 227 | The new syntax didn't test out to work. Waiting on confirmation |
| | 228 | of correct examples. |
| | 229 | |
| | 230 | #=head2 ref is gone |
| | 231 | |
| | 232 | Was: ref $foo eq 'HASH' |
| | 233 | Now: $foo ~~ 'Hash' |
| | 234 | |
| | 235 | Was: ref $foo eq 'ARRAY' |
| | 236 | Now: $foo ~~ 'Array' |
| | 237 | |
| | 238 | Was: ref $foo eq 'CODE' |
| | 239 | Now: $foo ~~ 'Code' |
| | 240 | |
| | 241 | The "obsolete" reference above has the details. |
| | 242 | |
| | 243 | =end TODO |
| | 244 | |
| | 245 | =head2 say() |
| | 246 | |
| | 247 | This is a version of print() that auto-appends a newline: |
| | 248 | |
| | 249 | Was: print "Hello, world!\n"; |
| | 250 | Now: say "Hello, world!"; |
| | 251 | |
| | 252 | Since you want to do that so often anyway, it seemed like a handy thing |
| | 253 | to make part of the language. |
| | 254 | |
| | 255 | =head1 Unfiled |
| | 256 | |
| | 257 | =head2 Hash elements no longer auto-quote |
| | 258 | |
| | 259 | Hash elements no longer auto-quote: |
| | 260 | |
| | 261 | Was: $days{February} |
| | 262 | Now: %days{'February'} |
| | 263 | Or: %days{"February"} |
| | 264 | Or: %days<February> |
| | 265 | Or: %days<<February>> |
| | 266 | |
| | 267 | The curly-bracket forms still work, but curly-brackets are more |
| | 268 | distinctly block-related now, so in fact what you've got there is a |
| | 269 | block that returns the value "February". The C<<>> and C<<<>>> forms |
| | 270 | are in fact just quoting mechanisms (see below). |
| | 271 | |
| | 272 | =head2 Built-in functions are now methods |
| | 273 | |
| | 274 | Most (all?) built-in functions are now methods of built-in classes such |
| | 275 | as String, Array, etc. |
| | 276 | |
| | 277 | Was: my $len = length($string); |
| | 278 | Now: my $len = $string.chars; |
| | 279 | |
| | 280 | Was: print sort(@array); |
| | 281 | Now: print @array.sort; |
| | 282 | @array.sort.print; |
| | 283 | |
| | 284 | You can still say C<sort(@array)> if you prefer the non-OO idiom. |