Similarities and Differences between JavaScript and Perl



JavaScript
Perl
"\x41" is the letter "A"
"\x41" is the letter "A"
" ' " is a single quote
' " ' is a single double quote
"\n" equals new line
'\n' also equals new line!
In JavaScript, there's absolutely no distinction
between single quotes and double quotes
" ' " is a single quote
' " ' is a single double quote
"\n" equals new line
'\n' is literally a backslash and a letter 'n'
Strings enclosed with single quotes are treated as literal
Statements SHOULD be ended with a semicolon,
but your program will still run if you leave them off.
Statements MUST be ended with a semicolon.
Function names and variable names MUST NOT start with @
All variable names and function names MAY start with a $ sign.
Function names MUST NOT start with @ or $
Scalar type variables MUST start with $ sign; arrays start with @
function test($A) { return $A * 1.3; }
or function test() { return arguments[0] * 1.3; }
Note: The $_[] array has no special meaning in JavaScript.
It may be used as an array name, but it must be first declared.
sub test { $_[0] * 1.3; }
Note: In Perl, $_[0] is a handle for the first argument of a sub.
Also, Perl automatically returns the value of the last statement,
so using the "return" keyword is not mandatory here.
// One line comment starts with double slashes
For multi-line comment, use /* ... */
# One line comment starts with a pound sign
There is no multi-line comment in perl, but
everything AFTER the __END__ tag in the
file is treated as a comment.
Internally, numbers are stored as IEEE-754 double precision
or as 32-bit signed integers (or BigInt string)
Numbers are stored as IEEE-754 double precision
AND as string AND as 32-bit or 64-bit signed integer
0.1 + 0.2 = 0.30000000000000004
0.1 + 0.2 = 0.3
Math.abs(-3);
Math.floor($FLOAT); or $FLOAT | 0
Math.ceil($NUMBER);
abs(-3);
int($FLOAT);
int($NUMBER+0.5);
There's no builtin ceil() function in Perl
Converting undefined to an empty string:
if (typeof($VALUE) == "undefined") $VALUE = "";
Converting undefined to an empty string:
defined $VALUE or $VALUE = "";
Merging two strings: $A + $B
Merging two strings: "$A$B" or $A . $B
Split expects a string:
ARRAY = $STRING.split("\n");
Split expects a regex:
@ARRAY = split(/\n/, $STRING);
Array names can start with any character
When declaring arrays, the name must start with the @ character
The sort() function returns the sorted list,
but it also changes the original array.
Usage: ARRAY.sort();
The sort() function returns the sorted list,
but does not change the original array.
Usage: @ARRAY = sort(@ARRAY);
Creating an Array
ARRAY = ["bird", "bug", "worm", 123, 444];
or ARRAY = "bird bug worm 123 444".split(" ");
Creating an Array
@ARRAY = ("bird", "bug", "worm", 123, 444);
or @ARRAY = qw(bird bug worm 123 444);
$ARRAY_LENGTH = ARRAY.length;
$ARRAY_LENGTH = @ARRAY;
$LAST_PTR = ARRAY.length - 1;
$LAST_PTR = $#ARRAY;
$B = A.push(1500);
The JavaScript push() function returns the element
that was pushed, in this case the number 1500
$B = push(@A, 1500);
The Perl push() function returns the new size of
the array AFTER the push is complete.
$B = A.pop();
$B = pop(@A);
$STRING = ARRAY.join("+");
$STRING = join("+", @ARRAY);
$B = $A.substr(2, 2);
$B = substr($A, 2, 2);
$B = $A.slice(7);
$B = substr($A, 7);
$B = $A.charCodeAt(22);
$B = vec($A, 22, 8);
or $B = ord(substr($A, 22, 1));
$B = $A.charAt(25);
$B = substr($A, 25, 1);
$C = String.fromCharCode(65);
$C = chr(65);
$P = $STRING.indexOf("/", $START);
$P = index($STRING, "/", $START);
$P = $STRING.lastIndexOf("/");
$P = rindex($STRING, "/");
$A = eval("1+1");
$A = eval("1+1");
$STRING = $STRING.replace(/[^A-Za-z]/g, "x");
$STRING =~ s/[^A-Za-z]/x/g;
if (/DOS|WIN/i.test($OS)) { ... }
if ($OS =~ m/DOS|WIN/i) { ... }
$STRING = $STRING.toUpperCase();
$STRING = $STRING.toLowerCase();
$STRING = uc($STRING);
$STRING = lc($STRING);
$HEX = $NUMBER.toString(16);
$OCT = $NUMBER.toString(8);
$BIN = $NUMBER.toString(2);
$HEX = sprintf("%X", $NUMBER);
$OCT = sprintf("%o", $NUMBER);
$BIN = sprintf("%b", $NUMBER);
$INTEGER = parseInt($HEX, 16);
$INTEGER = parseInt($OCT, 8);
$INTEGER = parseInt($BIN, 2);
$INTEGER = hex($HEX);
$INTEGER = oct($OCT);
$INTEGER = oct("0b$BIN");
$HEX = ("0000" + ($NUMBER &
0xffff).toString(16)).slice(-4);
$HEX = sprintf("%0.4X", $NUMBER);
$STRING = "ON SALE: Now $ " + $PRICE + " ea.";
$STRING = "ON SALE: Now \$ $PRICE ea.";
or $STRING = 'ON SALE: Now $ ' . $PRICE . ' ea.';
if ($A == 0) $A++;
or if (!$A) $A++;
if ($A == 0) { $A++; }
or if (!$A) { $A++; }
or unless ($A) { $A++; }
or $A || $A++;
...
location.href is a variable that
holds the full name of the HTML or HTA file
or if the JavaScript is running as a single file process,
then use WScript.ScriptFullName to get
the full name of the script.
$0 is a special variable that holds the name
of the Perl script that is running. Also: __FILE__
There is no builtin printf() and sprintf() function in JavaScript.
printf() and sprintf()
work just like in C / C++
There is no builtin pack() and unpack() function in JavaScript.
pack() and unpack()
There is no builtin tr/// operator in JavaScript.
tr///
There is no 'x' operator in JavaScript
and repeating characters is cumbersome.
var $LINE = (new Array(81)).join('-');
or var $LINE = ""; while ($LINE.length < 80) $LINE += "-";
my $LINE = '-' x 80;
There is no easy way to pad a string with zero bytes in JavaScript
You can either do this:
while ($STRING.length < 1024) $STRING += "\0";
or this: $STRING += (new Array(1025)).join("\0");
vec($STRING, 1023, 8) = 0;
Reading Files
File system access is not available when JavaScript is running
in a web browser. Otherwise in Windows, it's possible:
try { FSO = new ActiveXObject("Scripting.FileSystemObject");
FILE = FSO.OpenTextFile($FILENAME, 1);
$CONTENT = FILE.ReadAll();
FILE.Close(); } catch (e) { alert("Error: " +
"Can't open file for reading - " + $FILENAME); }
Reading Files
local *FH; open (FH, "<$FILENAME") or
die "\nError: Can't open file - $FILENAME\n";
binmode FH; my $CONTENT = <FH>; close FH;
for (var $i = 0; $i < 1000000000; $i++) { ... }

In JavaScript, the system becomes
unresponsive as long as a loop is running.
Whether you're reading a file or calculating something or
drawing on the screen, if a loop exists in your JavaScript
that is designed to run for more than 10 seconds,
there may be a noticeable decline in system performance.
for (my $i = 0; $i < 1000000000; $i++) { ... }

In Perl, you can have this loop running in the background,
and the system doesn't mind at all. Whether you're using
Linux or Windows, multitasking has a lot smoother experience
and is not a burden on the system.
JavaScript has no sleep function like Perl.
setTimeout("myFunc(1);", 100); myFunc(2);
This immediately calls myFunc(2)
and then 100ms later calls myFunc(1);
sleep(1); myFunc(1); myFunc(2);
This waits for 1 sec. then calls myFunc(1)
and then immediately calls myFunc(2)
Perl has no builtin function to sleep for less than 1 second.