Skip to content

Commit 03ba410

Browse files
committed
Add Node.js to Perl 6 nutshell page
Fixes #1968
1 parent 4ebcfc1 commit 03ba410

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

doc/Language/js-nutshell.pod6

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
=begin pod :tag<convert>
2+
3+
=TITLE Perl 6 from Node.js - Nutshell
4+
5+
=SUBTITLE Learning Perl 6 from Node.js, in a nutshell.
6+
7+
This page attempts to provide a way for users experienced in Node.js to learn
8+
Perl 6. Features shared between the two languages will be explained here, as
9+
well as major differences in syntax and features.
10+
11+
This is not a tutorial for learning Perl 6; this is a reference for users who
12+
are already at an intermediate to advanced skill level with Node.js.
13+
14+
=head1 Basic Syntax
15+
16+
=head2 "Hello, world!"
17+
18+
Let's start with the typical first program when learning new languages. In
19+
Node.js, a hello world program would be written like this:
20+
21+
=begin code :lang<javascript>
22+
console.log('Hello, world!');
23+
=end code
24+
25+
Here are a couple ways to write this in the same way in Perl 6:
26+
27+
=begin code
28+
say('Hello, world!');
29+
say 'Hello, world!';
30+
=end code
31+
32+
Parentheses are optional for function calls in Perl 6. While semicolons are,
33+
for the most part, optional in Node.js, they are mandatory for expressions in
34+
Perl 6.
35+
36+
Now that we've greeted the world, let's greet our good friend, Joe. We'll
37+
start with Node.js again:
38+
39+
=begin code :lang<javascript>
40+
let name = 'Joe';
41+
console.log('What\'s up,' + name + '?');
42+
console.log(`What's up, {name}?`);
43+
console.log("What's up, ", name, "?");
44+
=end code
45+
46+
Since he didn't hear us, let's greet him again, this time in Perl 6:
47+
48+
=begin code
49+
my $name = 'Joe';
50+
say 'What\'s up, ' ~ $name ~ '?';
51+
say "What's up, $name?";
52+
say "What's up, ", $name, "?";
53+
=end code
54+
55+
Here, there are only a couple differences: most variables in Perl 6 have what
56+
are called sigils, which are what the C<$> in front of its name is, and string
57+
concatenation uses the C<~> operator instead of C<+>. What the two languages
58+
share in common here is support for string interpolation.
59+
60+
Now that the basic examples are out of the way, let's explain the similarities
61+
between the two languages in greater detail.
62+
63+
=head2 Variables
64+
65+
Variables in Node.js can be defined like this;
66+
67+
=begin code :lang<javascript>
68+
var foo = 1; // Lexically scoped with functions and modules
69+
let foo = 1; // Lexically scoped with blocks
70+
const foo = 1; // Lexically scoped with blocks; constant
71+
72+
global.foo = 1; // Dynamically scoped; global
73+
foo = 1; // Ditto, but implicit; forbidden in strict mode
74+
=end code
75+
76+
In Perl 6 there is no equivalent to C<var>. An important note to make is that
77+
there is no variable hoisting in Perl 6; variables are defined and assigned
78+
at the line they're on, not defined at the top of its scope and later assigned
79+
at that line.
80+
81+
This is how the equivalent types of variables are defined in Perl 6:
82+
83+
=begin code
84+
my $foo = 1; # Lexically scoped with blocks
85+
our $foo = 1; # Lexically scoped with blocks and modules
86+
constant foo = 1; # Lexically scoped with blocks and modules; constant
87+
88+
my $*foo = 1; # Dynamically scoped with blocks
89+
OUR::<$foo> = 1; # Dynamically scoped with blocks and modules
90+
GLOBAL::<$foo> = 1; # Dynamically scoped; global
91+
=end code
92+
93+
Use C<my> where you'd use C<let>, C<our> for variables you'd define in the
94+
outermost scope needed, and C<constant> where you'd uses C<const>.
95+
96+
Dynamically scoped variables are not referred to in the same way as lexically
97+
scoped ones like they are in Node.js. User-defined ones use either a C<$*>,
98+
C<@*>, C<%*>, or C<&*> twigil. Refer to the documentation on
99+
L<variables|/language/variables> for more information on sigils, twigils, and
100+
variable containers.
101+
102+
Variables in Node.js can override others from outer scopes with the same name
103+
(though linters will usually complain about it depending on how they're
104+
configured):
105+
106+
=begin code :lang<javascript>
107+
let foo = 1;
108+
function logDupe() {
109+
let foo = 2;
110+
console.log(foo);
111+
}
112+
113+
logDupe(2); // 2
114+
console.log(foo); // 1
115+
=end code
116+
117+
Perl 6 also allows this:
118+
119+
=begin code
120+
my $foo = 1;
121+
sub log-dupe {
122+
my $foo = 2;
123+
say $foo;
124+
}
125+
126+
log-dupe; # 2
127+
say $foo; # 1
128+
=end code
129+
130+
=head2 Operators
131+
132+
=head3 Assignment
133+
134+
The C<=> operator works the same across both languages.
135+
136+
The C<:=> operator in Perl 6 binds a value to a variable. Binding a variable
137+
to another variable gives them the same value and container, meaning mutating
138+
attributes of one will mutate the other's as well. Bound variables cannot be
139+
reassigned with C<=> or mutated with C<++>, C<-->, etc. but they can be bound
140+
to another value again:
141+
142+
=begin code
143+
my %map; # This is a hash, roughly equivalent to a JS object or map
144+
my %unbound = %map;
145+
my %bound := %map;
146+
%map<foo> = 'bar';
147+
say %unbound; # {}
148+
say %bound; # {foo => bar}
149+
150+
%bound := %unbound;
151+
say %bound; # {}
152+
=end code
153+
154+
=comment TODO: operators for equality, bitwise, etc.
155+
156+
=head2 Conditionals
157+
158+
=comment TODO
159+
160+
# TBD
161+
162+
=head2 Functions
163+
164+
=comment TODO
165+
166+
# TBD
167+
168+
=head2 Types
169+
170+
=comment TODO
171+
172+
# TBD
173+
174+
=head1 Object-Oriented Programming
175+
176+
=comment TODO
177+
178+
# TBD
179+
180+
=head1 The Networking API
181+
182+
=comment TODO
183+
184+
# TBD
185+
186+
=head1 The File System API
187+
188+
=comment TODO
189+
190+
# TBD
191+
192+
=head1 Modules and Packages
193+
194+
=comment TODO
195+
196+
# TBD
197+
198+
=end pod
199+
200+
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)