Forgot your password?
typodupeerror
Perl Programming

Why JavaScript Is the New Perl 453

Posted by samzenpus
from the presto-chango dept.
theodp writes "'People are thoroughly excited [about JavaScript],' writes Lincoln Baxter. 'However, I'd akin this to people discovering Perl during the advent of C and C++ (mirror). Does it work? Yes. Is it pretty? Not by a long shot.' Baxter adds, 'While I do like both languages, JavaScript [is] just waiting for the next technology to come around and make it look like Perl does today: pervasive, but lacking enterprise adoption on large applications.'"
This discussion has been archived. No new comments can be posted.

Why JavaScript Is the New Perl

Comments Filter:
  • Modem noise (Score:4, Informative)

    by frisket (149522) <peter.silmaril@ie> on Sunday January 06, 2013 @07:01PM (#42498981) Homepage
    Yah. And Perl still looks like modem noise. Python is even worse. But JavaScript is like someone on acid tried to breed a zombie computer language in his basement.

    But, hey, lots of people like them, so they must be good, right? https://www.destroyallsoftware.com/talks/wat [destroyallsoftware.com]

  • Re:Modem noise (Score:5, Informative)

    by hobarrera (2008506) on Sunday January 06, 2013 @07:13PM (#42499063) Homepage

    I've heard many respectable criticisms of Python, but I've never heard anyone say it looks bad.
    Visually, it's probably the most elegant-looking language there is.

  • by CaptnCrud (938493) on Sunday January 06, 2013 @07:32PM (#42499167)

    Scenario, im new to perl. Would you consider this simple sort of subroutines human readable?:

    @s = sort mycriteria @a;
                  sub mycriteria {
                      my($aa) = $a =~ /(\d+)/;
                      my($bb) = $b =~ /(\d+)/;
                      sin($aa) sin($bb) ||
                      $aa*$aa $bb*$bb;
                  }

    I agree, that correct oop practices and not abusing special chars helps (the latter has always been rampant at every shop I ever worked at, as you said programmers are lazy), but make no mistake, the above is not readable to a beginner.

  • by mark-t (151149) <markt@@@lynx...bc...ca> on Sunday January 06, 2013 @08:27PM (#42499507) Journal

    The JS engines far outstrip any Java VM performance

    Citation please. I'll concede that modern JS engines are very good, but arguing that an engine for a dynamic interpreted language outperforms a modern VM for a statically typed compiled one sounds a bit.... suspicious.

  • by znrt (2424692) on Sunday January 06, 2013 @08:54PM (#42499649)

    the above is not readable to a beginner.

    The code above, expressed in any language, would be "not readable to a beginner." Sorting using custom comparison function requires little tweaks in pretty much any language.

    not in javascript:
    myarray.sort(function(a,b){return b - a}) ;

  • by CaptnCrud (938493) on Sunday January 06, 2013 @09:34PM (#42499901)

    This was ripped straight from John Klassa / Raleigh Perl Mongers / Sorting in perl.

  • Re:I don't.. (Score:3, Informative)

    by viperidaenz (2515578) on Sunday January 06, 2013 @10:16PM (#42500129)

    Web browsers are for browsing the web. http, ftp, gopher, mail, news. If they were supposed to be restricted to http, they'd be called http browsers.

    Web sockets were created because HTTP lacks efficient two-way communication. polling. long polling, comet, insert-new-buzzword-here are inefficient hacks to allow a client to be notified of a server generated event.

  • Perl's purpose (Score:5, Informative)

    by Darinbob (1142669) on Sunday January 06, 2013 @10:36PM (#42500247)

    Perl wasn't an alternative to C/C++. It became popular because it was a nice replacement for all those other Unix apps, a combination of sh+awk+sed, etc.

  • Re:I don't.. (Score:4, Informative)

    by IICV (652597) on Monday January 07, 2013 @02:26AM (#42501713)

    It just takes discipline- prepend your function names with the module name. If you're in namespace window and have a function getHeight, you write it as window_getHeight. It takes a little bit of discipline, but not that much.

    Whaaaaat. Why are you putting all your shit in the global scope in the first place? Use the module pattern [adequatelygood.com], and none of those functions leak out of the file you're in.

  • Re:I don't.. (Score:5, Informative)

    by IICV (652597) on Monday January 07, 2013 @02:40AM (#42501817)

    The weird scope rules and lack of proper object/class support drive me up the wall when working on projects with ~40,000 lines of code.

    I don't know, maybe I'm just weird myself but I don't think Javascript's scope rules are that hard to grasp, and the class support is workable but honestly most of the time you don't need to use classes at all.

    I mean, the scope is easy - are you in a function? If so, the variable is scoped to the function. If not, the variable is globally scoped. That's about it. Just wrap something like

    (function(){
    // code goes here
    }())

    around your script file, and everything will be local to that script file while still being able to see the global scope. There's some other funky stuff involving weird cases like using a locally scoped variable before it's defined, but as long as you're not writing crap on purpose it's not a huge deal.

    And as for classes - well, honestly, a lot of the time when you think "I need a class for this", what you really mean is "I've got some data that needs to travel together". In Javascript, you can just do that - you can just say:

    //here's a thing to hold my data
    var thing = {};
    //here's data to put in the thing
    thing.item1 = "something";
    //here's some more related data
    thing.item2 = {message: "I'm important!"};

    And then tada! You've got a class that carries your data around. Hooray!

    But if you really want classes, with methods and stuff like that, they're there too [stackoverflow.com] - but if you're writing a project large enough to actually need those sorts of tools, you really ought to be using a framework that'll handle the nitty-gritty of classes for you.

  • Re:I don't.. (Score:4, Informative)

    by Archibald Buttle (536586) <`steve_sims7' `at' `yahoo.co.uk'> on Monday January 07, 2013 @06:31AM (#42503021)

    I think that the problem you have is that JavaScript doesn't match your experience and/or expectations of how a programming language should work.

    The scope rules of JavaScript are actually very straight forward. The problem is that most languages have block-based scope. JavaScript instead has function-based scope.

    As for proper "object/class" support, well, classes are just one way of doing objects, and not the only way. JavaScript has first-class support for objects. It's inheritance model however is prototype-based, rather than class-based. It's a different paradigm, but no less valid.

    Your experience and expectations of how a language should work (probably based on experience writing C++, Java, or any number of other languages) say that scope must be block-based, and object models must be class based. Those aren't the only solutions though.

  • Re:Readability (Score:4, Informative)

    by lattyware (934246) <gareth@lattyware.co.uk> on Monday January 07, 2013 @06:55AM (#42503141) Homepage Journal

    Of course it is possible to write unclear code in Python - preventing that would be impossible while retaining a useful language.

    The whole program sits wrongly with me - for one, the user is using io.open() instead of the open builtin - there is no good reason to do that. Next they are opening files and relying on the garbage collector to close them (not using the with statement as would be sensible), then they use string operations on data they have told us is binary data, use a nested loop in a generator expression, which yes, is ugly, instead of using itertools.chain() to flatten the iterable.

    So how would I write the same program?

    import itertools

    with open("even.bin", "rb") as even, open("odd.bin", "rb") as odd, open("interleaved.bin", "wb") as interleaved:
    for item in itertools.chain(zip(even, odd)):
    interleaved.write(data)

    itertools.chain() is the more efficient and readable way of flattening a 2-layer iterable, instead of the 'horror' you point out. This version also writes bit by bit, rather than joining in-memory, meaning that it doesn't load the entire thing into memory, meaning it will work with extremely large input files (larger than system memory). The use of the with statement means that the files will be closed even if an exception occurs. Plus, the whole thing is more readable.

    Python is much more conducive to writing legible code - note neither example involves lots of indices on a list, for example. It's not a miracle worker, but given people who have taken a bit of time to learn the language, it makes writing very readable code very easy. That's a great thing - it's not an instant fix, but trust me when I say I know Java just as well and yet I could never write more readable Java than I could Python for any given task.

I bet the human brain is a kludge. -- Marvin Minsky

Working...