5
5
= SUBTITLE How do I do what I used to do? (Perl 6 in a nutshell)
6
6
7
7
This page attempts to provide a fast-path to the changes in syntax and
8
- semantics from Perl 5 to Perl 6. Whatever worked in Perl 5 and must be written
9
- differently in Perl 6, should be listed here (whereas many I < new > Perl 6
10
- features and idioms are not).
8
+ semantics from Perl 5 to Perl 6. Whatever worked in Perl 5 and must be
9
+ written differently in Perl 6, should be listed here (whereas many
10
+ I < new > Perl 6 features and idioms are not).
11
11
12
- Hence this should not be mistaken for a beginner tutorial or a promotional
13
- overview of Perl 6; it is intended as a technical reference for Perl 6
14
- learners with a strong Perl 5 background and for anyone porting Perl 5 code
15
- to Perl 6 (though note that L < #Automated translation > might be more
16
- convenient).
12
+ Hence this should not be mistaken for a beginner tutorial or a
13
+ promotional overview of Perl 6; it is intended as a technical reference
14
+ for Perl 6 learners with a strong Perl 5 background and for anyone
15
+ porting Perl 5 code to Perl 6 (though note that L < #Automated
16
+ translation > might be more convenient).
17
17
18
18
A note on semantics; when we say "now" in this document, we mostly just
19
19
mean "now that you are trying out Perl 6." We don't mean to imply that
20
20
Perl 5 is now suddenly obsolete. Quite the contrary, most of us love
21
21
Perl 5, and we expect Perl 5 to continue in use for a good many years.
22
- Indeed, one of our more important goals has been to make interaction between
23
- Perl 5 and Perl 6 run smoothly. However, we do also like the design
24
- decisions in Perl 6, which are certainly newer and arguably better
25
- integrated than many of the historical design decisions in Perl 5.
26
- So many of us do hope that over the next decade or two, Perl 6 will
22
+ Indeed, one of our more important goals has been to make interaction
23
+ between Perl 5 and Perl 6 run smoothly. However, we do also like the
24
+ design decisions in Perl 6, which are certainly newer and arguably
25
+ better integrated than many of the historical design decisions in Perl
26
+ 5. So many of us do hope that over the next decade or two, Perl 6 will
27
27
become the more dominant language. If you want to take "now" in that
28
28
future sense, that's okay too. But we're not at all interested in the
29
29
either/or thinking that leads to fights.
30
30
31
31
= head1 CPAN
32
32
33
- See L < https://modules.perl6.org/ > .
33
+ See L < https://modules.perl6.org/ > .
34
34
35
- If the module that you were using has not been converted to Perl 6, and no
36
- alternative is listed in this document, then its use under Perl 6 may not
37
- have been addressed yet.
35
+ If the module that you were using has not been converted to Perl 6, and
36
+ no alternative is listed in this document, then its use under Perl 6 may
37
+ not have been addressed yet.
38
38
39
39
The L < Inline::Perl5|https://github.com/niner/Inline-Perl5/ > project makes
40
40
it possible to C < use > Perl 5 modules directly from Perl 6 code by using
@@ -1477,10 +1477,9 @@ The L«C<FALLBACK> method|/language/typesystem#index-entry-FALLBACK_(method)»
1477
1477
provides similar functionality.
1478
1478
1479
1479
= head2 Importing specific functions from a module
1480
- X < |import >
1481
1480
1482
- In Perl 5 it is possible to selectively import functions from a given module
1483
- like so:
1481
+ In Perl 5 it is possible to selectively import functions from a given
1482
+ module like so:
1484
1483
1485
1484
= for code :lang<perl5>
1486
1485
use ModuleName qw{foo bar baz};
@@ -1498,23 +1497,24 @@ sub bar($b) is export { say "bar $b" }
1498
1497
sub baz($z) { say "baz $z" }
1499
1498
= end code
1500
1499
1501
- To use this module, simply C < use Bar > and the functions C < foo > and C < bar >
1502
- will be available
1500
+ To use this module, simply C < use Bar > and the functions C < foo > and
1501
+ C < bar > will be available
1503
1502
1504
1503
= for code :skip-test
1505
1504
use Bar;
1506
1505
foo(1); #=> "foo 1"
1507
1506
bar(2); #=> "bar 2"
1508
1507
1509
- If one tries to use C < baz > an "Undeclared routine" error is raised at compile time.
1508
+ If one tries to use C < baz > an "Undeclared routine" error is raised at
1509
+ compile time.
1510
1510
1511
- So, how does one recreate the Perl 5 behaviour of being able to selectively
1512
- import functions? For this one needs to define an C < EXPORT > sub inside the
1513
- module which specifies the functions to be exported and remove the C < module Bar >
1514
- statement.
1511
+ So, how does one recreate the Perl 5 behaviour of being able to
1512
+ selectively import functions? For this one needs to define an C < EXPORT >
1513
+ sub inside the module which specifies the functions to be exported and
1514
+ remove the C < module Bar > statement.
1515
1515
1516
- The module C < Bar > now is merely a file called C < Bar.pm > with the following
1517
- contents:
1516
+ The module C < Bar > now is merely a file called C < Bar.pm > with the
1517
+ following contents:
1518
1518
1519
1519
= begin code :preamble<no strict;>
1520
1520
use v6.c;
@@ -1538,14 +1538,16 @@ sub bar($a) { say "bar, $a" }
1538
1538
sub baz($z) { say "baz, $z" }
1539
1539
= end code
1540
1540
1541
- Note that the subs are no longer explicitly exported via the C < is export >
1542
- role. We are defining an C < EXPORT > sub which specifies the subs in the
1543
- module we want to be available for export and then we are populating a hash
1544
- containing the subs which will actually be exported. The C < @import-list > is
1545
- set by the C < use > statement in the calling code thus allowing us to
1546
- selectively import the subs made available by the module.
1541
+ Note that the subs are no longer explicitly exported via the C < is
1542
+ export > role. We are defining an C < EXPORT > sub which specifies the subs
1543
+ in the module we want to be available for export and then we are
1544
+ populating a hash containing the subs which will actually be exported.
1545
+ The C < @import-list > is set by the C < use > statement in the calling code
1546
+ thus allowing us to selectively import the subs made available by the
1547
+ module.
1547
1548
1548
- So, to import only the C < foo > routine, we do the following in the calling code:
1549
+ So, to import only the C < foo > routine, we do the following in the
1550
+ calling code:
1549
1551
1550
1552
= for code :skip-test
1551
1553
use Bar <foo>;
0 commit comments