Greetings, Monks.

As I toyed with the notion of creating a JavaScript to Perl converter; I stumbled across a table. While seasoned veterans of both languages may not have a need for it. I thought it still might be a nice reference, for quick-n-easy comparison of both languages. To others needing this sort of information.

You can find it here: Equivalents in JavaScript and Perl

Best wishes.

--Chris

EDIT: corrected typo to the external link -- thanks zentara. :)
#!/usr/bin/perl -Tw
use Perl::Always or die;
my $perl_version = (5.12.5);
print $perl_version;
  • Comment on A comparison of Perl vs. JavaScript -- a reference table.

Replies are listed 'Best First'.
Re: A comparison of Perl vs. JavaScript -- a reference table.
by zentara (Archbishop) on Dec 05, 2013 at 20:18 UTC
      Thats a very rough comparison, for instance the differences of my and var and their scoping mechanisms can lead to ugly problems.

      E.g. JS knows only function scope and not block scope AND unlike my does var also influence preceding variables (so called "hoisting")

      Further more linking to MDN instead of W3Schools for further informations is generally a better idea.

      For instance see Mozillas approach to come closer to my by introducing "let"!

      For those who can read German here an old talk of mine from GPW2011 investigating the differences.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      ) but unfortunately still hoisting =(

        Thank you very much for your insight on that table, LanX.

        I think I'll make a copy, and see if I can't improve, it based on your, and perhaps others, input -- not to mention, my own. ;)

        Thanks again, for taking the time to respond, LanX. I look forward to reading your talk.

        Best wishes.

        --Chris

        EDIT: Oh. If I could only read the German language. Or (Ich bin nicht in der Lage zu lesen der deutschen Sprache.).
        I don't suppose you have an English translation of your talk, LanX?

        Hey. I'm not completely useless. I can be used as a bad example.
        

        Further more linking to MDN instead of W3Schools for further informations is generally a better idea.

        FWIW, this guy has been using MDN all this time, but its broken again. This time for firefox by javascript (html content shows, then blanked out ). Possibly by googleanalytics , lots of js hate for ol firefox

        Never had issues like this with W3schools even though I stopped using it long ago

        Internet, its a thing :)

Re: A comparison of Perl vs. JavaScript -- a reference table.
by harangzsolt33 (Friar) on Dec 15, 2020 at 00:53 UTC
    I'm coming from the other direction. I learned JavaScript first, and then Perl. Actually, the very first language I learned was BASIC, but it's amazing how many commonalities these languages share. Perl is a bit more like BASIC in the way it treats strings. You can do something like this : MID$(A$, 8, 1) = "A" in BASIC, which is the same as vec($A, 8, 8) = 65; in Perl. However, you can't do this in JavaScript. To achieve it anyway, you have to split the original string into two parts and add them together with the new "A" in the middle. This requires more memory, of course.

    In Perl, you can access the command line through the @ARGV array. In BASIC, you can do it from COMMAND$ and there's a way to do it in JavaScript as well, but it varies depending on the application. For example in a HTA program, you would do it like this:

    <HTA:APPLICATION ID="objTestHTA" ...>
    </HEAD>

    <SCRIPT>

    alert( objTestHTA.commandLine );

    </SCRIPT>

    If you happen to write JavaScript for Node.js environment, then you'd use an array called process.argv to access the commandline arguments...

      Perl was the scripting language back then, and Brendan Eich admitted once that he was strongly influenced by Perl (that interview is hard to find now). This influence is more than obvious if you look at the Array methods.

      The story goes (IIRC):

      He was initially hired to write a Scheme like dialect. Many applications had embedded Lisp engines, like Emacs, AutoCad, Gimp,...

      But Netscape made a deal with Sun, and he was told to design something that looks syntactically like Java.

      As you mentioned , are strings one important difference, because they are immutable in JS, while in Perl they are a piece of memory at your free disposal.

      The lack of pure hashes is (or was?) a big problem in JS.

      There are some other issues, but for me is 90% of JS just a mini Perl with Java syntax.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        I heard him tell the story at CurryOn 2015 in Prague. IIRC, he was not only hired to write a Scheme like dialect, but he actually wrote it that way. When he was almost done like a week before the contract end, they asked him to change the syntax to be more Java-like.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: A comparison of Perl vs. JavaScript -- a reference table.
by harangzsolt33 (Friar) on Jan 03, 2021 at 02:57 UTC
    If you ever decide to create your JavaScript to Perl converter, here are a couple of things I learned:

    If you need to work with a long string, replacing characters, inserting strings, splitting/joining strings, then in JavaScript it is a lot faster to convert the string into an array of characters and then work on the array elements one by one. The split() and join() functions are extremely fast in JavaScript, however if this code is converted into Perl, it becomes very slow! I've noticed that Perl code runs faster if you leave the string as a string and work on it by replacing characters using the vec() and substr() functions. Perl is better with strings than arrays. Having a big array in Perl just slows things down. In JavaScript, it's just the opposite!

    Secondly, the % operator works quite differently in Perl than in JavaScript! Consider the following JavaScript code:

    C = A % B;

    This translates to the following Perl code:

    $C = $A - int($A / $B) * $B;

    So, this operator is tricky! It's easy to overlook this difference.

      Strings are immutable in JS, e.g. a regex replace there requires an assignment to a new variable.

      I.e. it's always s///r there.

      I'm not sure if I'm still up to date, but the internal text format in JS is UTF 16 while it's kind of UTF 8 in Perl. This requires 2 or 4 bytes /character in JS. So more memory needed but faster to split.

      5 % 2.2 is 1 in Perl but ~0.6 in JS. Perl accepts only the integer part of the arguments. It's documented in perlop but still surprising.

      Please note that 50%22 is 6 in both languages.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      ) Well IFF the utf8 flag is enabled in Perl that is.