📜 ⬆️ ⬇️

JavaScript - a quick start for Perl developers

Perl and JavaScript are very similar.

Elementary



The arrays and hashes of JavaScript are almost identical to the links to the arrays and the links to the hashes of the Perl language. The only thing - as a separator => separator appears:.

Compare Perl
my $a = [ 1, 4, 67, { x => 67, y => 43} ];

and javascript
var $a = [ 1, 4, 67, { x: 67, y: 43} ];

')
Two important differences:
* Enumeration of hash elements in JavaScript should not end with a comma (this will lead to an error). For an array, the definition var a = [1,2,45,67,];
* For javascript variables there is no concept context and prefix. $ Is just the standard allowed character for a variable name. In the working code, the $ sign is best used only to refer to the corresponding. jQuery / Prototype functions.

Scalar values ​​in JavaScript are a bit more typed (the numeric, string, and boolean types are separated), but this is a scripting language, and anything can also be found in a variable.

Important note: The internal format of the javascript string is unicode. All built-in JavaScript escaping functions know only the utf-8 encoding. I recommend for Ajax-interaction, respectively, to choose only the utf-8 encoding.

Functions in JavaScript and subroutines in Perl are very similar. Both those, and others allow to operate with short circuits. Both are simple variables, with the possibility of an alternative form of recording. The internal function operates on the variables of the external function.

Compare Perl
my $f = sub
{
my ($a, $b) = @_;
return $a * $b;
};


and javascript
var $f = function($a, $b)
{
return $a * $b;
};


A slight difference: with this form of defining a function in JavaScript, the semicolon is not required after the closing brace. But I recommend always using just such a form of function definition, and include a semicolon (for packer to work correctly).

An important difference: In JavaScript, functions, as well as arrays, and hashes are special cases of objects. All JavaScript objects are actually hashes. The a ['b'] record is identical to the ab record (the key presence checker is available 'b' in a).

Namespaces and Classes



Perl packages are very conveniently supported in JavaScript through namespace functions.
Perl:
package Something;

sub x
{
}


Javascript:
window.Something = function()
{
var x = function()
{
};

};
window.Something.x = x; //


The object model in JavaScript can be supported in a variety of ways, during the seminar I told only about one of them - this is the formation of an object by a constructor function.

After a brief introduction to jQuery, an example plugin was introduced:
(function() // .
{
$.fn.helloWorldControl = function()
{
this.empty();
// , .
// ,
// .
var div = $('Hello, world').appendTo(this);
};
})();


We use:
$('#example').helloWorldControl();


Regular expressions



The good news is that regular expressions are supported by JavaScript. The bad news is using them is a little more difficult.
Perl:
if ($v =~ m/$began/i)
{
}


Javascript:
if (v.match(/$began/i))
{
}


Perl:
$v =~ s/$began/begin/i;


Javascript:
v.replace(/$began/i, 'begin');


One of the readers remembered a wonderful article by Dmitry Koterov: Little tricks of JavaScript, or writing scripts in a new way .

I also recommend the RSDN: introduction to jQuery .

PS:
In the next article I will try to talk about an easy and convenient RPC between JavaScript and PHP / Perl and .NET.

PPS:
It so happened that in my company I am a carrier of knowledge mainly on client side technologies (DHTML, JavaScript, jQuery, Ajax), at the same time possessing some basic Perl skills.

However, knowledge of client technologies is now very important for most Perl programmers, who previously used simple page generation with patterns.

As a result, this article was born (originally a seminar).

Source: https://habr.com/ru/post/26660/


All Articles