summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFather Chrysostomos <sprout@cpan.org>2013-06-03 20:48:00 -0700
committerFather Chrysostomos <sprout@cpan.org>2013-06-03 21:31:59 -0700
commit9b12f83b0b65827942028a14ac697977b5a83f3f (patch)
tree9fb4cdab7ea4f9f9c430630619b964e06e106458
parent1b45d357ddcec6f3ef5edd084309d7474c6041e4 (diff)
downloadperl-9b12f83b0b65827942028a14ac697977b5a83f3f.tar.gz
Traps for the unwary JS programmer (perltrap.pod)
This is part of ticket #117507 or #109408, whichever you like. This incorporates suggestions and corrections from Ronald Kimball and Tom Christiansen. Thank them!
-rw-r--r--pod/perltrap.pod95
1 files changed, 95 insertions, 0 deletions
diff --git a/pod/perltrap.pod b/pod/perltrap.pod
index ee17470266..d55e77a6b0 100644
--- a/pod/perltrap.pod
+++ b/pod/perltrap.pod
@@ -276,6 +276,101 @@ for numeric comparisons.
=back
+=head2 JavaScript Traps
+
+Judicious JavaScript programmers should take note of the following:
+
+=over 4
+
+=item *
+
+In Perl, binary C<+> is always addition. C<$string1 + $string2> converts
+both strings to numbers and then adds them. To concatenate two strings,
+use the C<.> operator.
+
+=item *
+
+The C<+> unary operator doesn't do anything in Perl. It exists to avoid
+syntactic ambiguities.
+
+=item *
+
+Unlike C<for...in>, Perl's C<for> (also spelled C<foreach>) does not allow
+the left-hand side to be an arbitrary expression. It must be a variable:
+
+ for my $variable (keys %hash) {
+ ...
+ }
+
+Furthermore, don't forget the C<keys> in there, as
+C<foreach my $kv (%hash) {}> iterates over the keys and values, and is
+generally not useful ($kv would be a key, then a value, and so on).
+
+=item *
+
+To iterate over the indices of an array, use C<foreach my $i (0 .. $#array)
+{}>. C<foreach my $v (@array) {}> iterates over the values.
+
+=item *
+
+Perl requires braces following C<if>, C<while>, C<foreach>, etc.
+
+=item *
+
+In Perl, C<else if> is spelled C<elsif>.
+
+=item *
+
+C<? :> has higher precedence than assignment. In JavaScript, one can
+write:
+
+ condition ? do_something() : variable = 3
+
+and the variable is only assigned if the condition is false. In Perl, you
+need parentheses:
+
+ $condition ? do_something() : ($variable = 3);
+
+Or just use C<if>.
+
+=item *
+
+Perl requires semicolons to separate statements.
+
+=item *
+
+Variables declared with C<my> only affect code I<after> the declaration.
+You cannot write C<$x = 1; my $x;> and expect the first assignment to
+affect the same variable. It will instead assign to an C<$x> declared
+previously in an outer scope, or to a global variable.
+
+Note also that the variable is not visible until the following
+I<statement>. This means that in C<my $x = 1 + $x> the second $x refers
+to one declared previously.
+
+=item *
+
+C<my> variables are scoped to the current block, not to the current
+function. If you write C<{my $x;} $x;>, the second C<$x> does not refer to
+the one declared inside the block.
+
+=item *
+
+An object's members cannot be made accessible as variables. The closest
+Perl equivalent to C<with(object) { method() }> is C<for>, which can alias
+C<$_> to the object:
+
+ for ($object) {
+ $_->method;
+ }
+
+=item *
+
+The object or class on which a method is called is passed as one of the
+method's arguments, not as a separate C<this> value.
+
+=back
+
=head2 Perl Traps
Practicing Perl Programmers should take note of the following: