Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Which scripting language do I start with?

  1. Feb 9, 2009 #1

    I've been programming for some time now, and I've learned the so-called industrial languages, like C, C++ and Java. Now I want to pick up a scripting language, but I'm getting confused by the variety. Even if I take the most well-known options, there are Perl, Python, Ruby and PHP. Which of these is better to learn first? Thanks.

  2. jcsd
  3. Feb 10, 2009 #2
    Having used all four, I'd say:

    Python is the simplest, it is also the most java-like. I would suggest starting there.
  4. Feb 10, 2009 #3
    I was also leaning towards Python a bit. Why is PHP preferred for web development?

  5. Feb 10, 2009 #4
    Hi loom,

    I probably shouldn't try to speak for other people, but there are two reasons I think why PHP has such incredible popularity for web development these days.

    One is that PHP files actually are basically HTML files with embedded code, like JSPs. So like

    Code (Text):
    <html><title>A random number between 1 and 10</title>
    <?php echo rand(1,10) ?>
    is a PHP program. I think a lot of people find this approach to web programming easier than having to litter their code with PRINT statements or whatever.

    The other reason is that PHP is extremely easy to learn, especially for people with no previous exposure to a programming language. Its data types and basic structures are extremely simple, and subtly molded to the tasks you're most likely to encounter in day-to-day web programming. Someone who advocates PHP over, say, Python might argue that Python expects a bit too much of you, that it is too strict or makes things too complicated if you are just writing a simple CGI script. With PHP you just pick it up and you're moving immediately.

    The problem, in my experience, is that PHP does not scale up-- it makes the easy things super easy, but it makes difficult things in some ways more difficult. Someone who has previous experience with a C-like programming language may find PHP too simple, simplistic maybe. The same tools that seemed sublimely convenient for the basic construction of a web page turn out to behave in occasionally strange and frustrating ways when you start using them for more complex tasks. Then there is the question of versatility. Although Perl, Python and Ruby have healthy communities using it for a wide variety of applications including but not limited to CGI, PHP basically doesn't exist outside of web programming. There are a wealth of libraries and frameworks for using PHP for web work, but if the scope of your PHP program starts to broaden to something not normally part of web programming, you may find it difficult to find adequate support or libraries to implement that part of the program; if this happens you will have to do it yourself, and you will be doing it in a language that is in some ways unhelpful when you go outside of the simple web programming tasks it was sculpted for.

    This is my opinion at least.
  6. Feb 10, 2009 #5


    User Avatar
    Science Advisor
    Homework Helper

    I agree with your analysis. I learned PHP and wrote some simple programs (webpage commenting software, with security of course) and found it very easy to use. But for anything beyond basic projects it becomes a real pain.
  7. Feb 11, 2009 #6
    Depends what you want. I suggest the phrase "scripting language" is misleading, as it doesn't describe a language as much as a particular way of using it. It's hard to argue, for instance, that python isn't an "industrial" language suitable for extremely large applications as well as small scripts. (Ask YouTube).

    Perl is probably the best for working with text data, although as a whole it is a badly designed and severely broken language. I think largely obsolete for general-purpose programming. Its syntax is very odd, which partly explains its steep learning curve. Ruby is related to Perl, and shares a large subset of its syntax in common. Its implementations are very poor, though. Python is an extremely good general-purpose language with no major flaws, and I'd recommend it over the other two.

    Python is absolutely the opposite of Java. Python has dynamic type inference, as opposed to Java which infers types at compile-time and requires static type annotations. Python has dynamic objects, which can be modified at run-time (creating new variables, defining new methods, etc.) Without static typing, there is no formal method of class inheritance: "duck typing" replaces the notion of "is a" with "can function like a". Python supports functional programming, treating functions as first-class objects: functions as arguments and return values, functions which manipulate functions, dynamically-defined functions (closures), and so on. Python's syntax is designed to be very concise, whereas Java is famous for being verbose: Python does not use curly brace block delimiters, nor semicolons for expressions; it has native syntax for lists, strings, hashes (not class syntax); it has no type annotations; it does not require explicit 'return's, and so on. Python is an interpreted language: programs can be run interactively in a "read-eval-print" loop, which completely changes the nature of debugging.

    Java isn't anything like this:

    Code (Text):

    Class Cat:
       def meow(self):
          print "meow!"

    kitty = Cat()
    kitty.meow()  # meow!

    def bark():
       print "woof!"

    kitty.bark = bark
    kitty.bark()   # woof!

    print dir(kitty)   # ['__doc__', '__module__', 'bark', 'meow']
    Or this:

    Code (Text):
    def twice(f):
       def ff(x):
          return f(f(x))
       return ff

    def square(x):
       return x*x

    print twice(square)(5)  # (5^2)^2 = 625
    Or this:

    Code (Text):
    def integrate(f, low, high, res):
       dx = high - low
       if (dx < res):
          return f((high+low)/2.0) * dx
       return integrate(f, low, high-dx/2, res) + integrate(f, low+dx/2, high, res)

    def square(x):
       return x*x

    print integrate(square, 0.0, 1.0, 0.0001)  # 0.333333333023

    import math
    print integrate(math.sin, 0, math.pi, 0.0001)  # 2.00000000077
    Or this:

    Code (Text):
    print reduce(lambda a,b: a*b, range(1,11)) # 10! = 3628800
    Or this:
    Code (Text):
    a, b, c = 10, 20, 30
    print b  # 20

    PHP was designed for web development, but its being the most popular is mostly a historical accident.

    No one uses "PRINT" statements: every language used by web developers has templating systems analagous to PHP's, where one embeds dynamically-created content in a "skeleton" of HTML. Here's Django templates. (Non-framework) PHP is actually much weaker with templating, because it mixes the application logic with the template - there is no distinct templating system. Often PHP programs combine application logic and display logic in ridiculous spaghetti code - usually with SQL, CSS, and javascript mixed in as well. I would recommend starting web programming with a framework, as opposed to raw PHP (PHP has frameworks too). A framework actively enforces abstraction barriers, avoiding bad habits. Something like the Django framework for Python, or Rails for Ruby.
    Last edited: Feb 11, 2009
  8. Feb 11, 2009 #7
    Wow, thanks signerror and Coin, really helpful replies. It seems to me from your examples that a lot of Python's power (apart from the convenient syntactical sugars) comes from it allowing a functional programming approach. So I think I'll start learning Python after I pick up a little purely functional programming with Scheme from The Structure and Interpretation of Computer Programming.

  9. Feb 12, 2009 #8
    Oh, it's not just functional programming - that's just a tiny part of it, and not even that good. (They actually scaled it back in Python 3, relegating some of the features to modules). The dynamic nature - dynamic type checking, dynamic, run-time modifiable objects, introspection (objects that can examine their own structure) is the big strength. Nice syntax, overall language consistency and design, all together it's a very good language.

    Scheme is a much stronger FP language, and it is also much smaller (it's designed for academic use). SICP is an extremely good book, although I should warn you it's written for university computer science majors, and as such is much "heavier" than your usual O'Reilly programming language book. It's not about functional programming either.
    Last edited: Feb 12, 2009
  10. Feb 13, 2009 #9
    I'm not really worried about the heaviness, I'm sure I can handle SICP after having dealt with the abstraction of real analysis. But signerror, why do you say that SICP is not about functional programming?

  11. Feb 14, 2009 #10
    Because while it is immersed in the functional style, that is not its main focus.

    Don't forget, the full text is free online:

  12. Mar 13, 2009 #11
    If you can avoid PHP, do. It's a monstrous but occasionally useful language. Perl always looked like a bit like Scientology: normal people avoid it, there are a ton of horror stories, and a few rock stars who think it's awesome. Python and Ruby it is then.

    I love Ruby a lot. Do a lot of work in it. The Ruby 1.8 interpreter isn't very quick, although Ruby 1.9 is apparently a lot better, as are the Ruby-atop-VM solutions like JRuby (for Java) and IronRuby (for .NET/Mono etc.), plus Rubinius (aka. 'rbx') is meant to be very good.

    I'd say that Ruby is the purer of the languages: it has a nicer syntax, and, in my experience, requires a lot less time looking stuff up in books and docs - it's just natural. Python, on the other hand, is a bit quicker, has proper Unicode support, and is institutionally a safer choice - with a lot more large companies (Google), universities and so on adopting it.

    Here's some Ruby code I wrote recently:
    Code (Text):
    (1..49).to_a.sort_by{ rand }.slice(0..6).sort
    It's basically a lottery picker. It picks six distinct integers between one and forty-nine and returns them as a sorted array. The (1..49) is a Range object, and the .to_a is a message being sent to that object, which returns an Array object. The sort_by method is being called on the Array - it's inherited from Enumerable - and for each value in the array, it is passed to the Proc - { rand } - Ruby's implementation of lambda. Each time { rand } is called, it returns a random number which is used as the sort value for the array. It returns a new array sorted. Then the slice method takes the first six items from the list and returns them as a slice of the array. Then they are sorted and returned.

    If I wanted to print this lot, I would simply prepend it with "puts " and append ".join(" ")

    Object-oriented one-liners. Awesome, no?

    I know Python, but hardly ever use it any more. I tend to code small hacky stuff in Ruby, and use it for prototyping. I use Python for stuff where there are libraries only available in Python - rdflib for example, Z39.50 hackery and so on. Stuff that needs a bit more performance tends to get rewritten in the Java language. I've got a large corpus of text that needed processing. I prototyped a solution in Ruby which was taking about five seconds for each iteration, and it had just under 78k iterations - the whole thing would take four and a half days (given five seconds * 78k). Rewrote it differently in Java, and the whole run took eight minutes on a not-particularly-nippy old Linux server. The difference between eight minutes and four days is important: it allows mistakes. If I screw something up, I can change it, recompile and re-run in in a way I can't if I have to wait for it.
  13. Mar 13, 2009 #12
    Got an e-mail notification saying signerror asked how I know that the { rand } proc gives a uniformly random permutation? Answer: I know it doesn't. It's not very random at all, and if it were for anything other than picking six lottery numbers, you'd want to use something that was. This post on eigenclass.org describes the problem and the solution.

    I find that I've never needed to worry about the absolute randomness of the selection, since I don't build anything which needs it. If I wanted something more random than sort_by.{ rand }, I'd probably use JRuby and Google "java randomness". ;)
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Which scripting language do I start with?