If perl is write-only…

Posted by Chris on September 3rd, 2009 filed in programming

Then python is read-only.  Think of it.

Both have a shebang line, and after that import (use) lines.  Perl’s are mostly optional – for sysadmin stuff you’re usually just doing your boilerplate strict and warnings.  Of course, even that is optional.  Technically anyway.  For python, you need to import something to do absolutely anything.   Which is okay – it shows you what is being used.

Then on to the real work.  In perl, you start out with the program.  It’s right there.  If you want to see the logic, just open the file – it’s usually at the top.  Python is the opposite – you have to declare your objects and functions higher up in the file before you can use them.   I couldn’t say you have to declare them before you use them because in practice you’re coding along and think “hey, this should be a function” and zoom down a bit and add it, then go back to the logic.  You’re still doing them before.

So you have your listing of objects and functions somewhere, and the actual program logic somewhere.  But this shows one difference between the two.

Perl cares about doing things.  Python cares about defining things.


3 Responses to “If perl is write-only…”

  1. CBT Says:

    Interesting post, even if it was a rant. A few random thoughts…

    Python is read only? I’ll accept that with as much seriousness as I accept Perl being write only. That is, none. It may be a fun dig, but it doesn’t really hold up to much analysis.

    Functionally, I think Python is very similar to Perl. There are a lot of cultural differences, but the technical differences are negligible.

    Oh, also, when I’m writing a command line program in Python, I like to have a main function that does all the actual work. That way I can define it at the top of the code and it isn’t hidden by my other function definitions.

  2. James William Pye Says:

    Python is my high level language of choice, and I’ll concede that Perl is probably a better tool for sysadmins. If you were looking for a quick and ez-way to run some regex’s and pipe data through some processes/command, Python will be relatively “long-winded”.

    However, the second that you come to a place where you need to construct a documented application that will need to be maintained for years, I would strongly recommend that you carefully consider Python. Those details that tend to get in your way with quick scripts are likely the very formalities that tend to help you maintain a full-scale application. (sure, it’s just opinion =)

    And a clarification: an object needs to exist before it’s used. ie, In a function definition, you can reference a name from the global scope before it exists, but when you invoke that function, it had better be resolvable:

    def foo():
    return bar()
    def bar():
    return 1
    print(foo())
    del bar
    print(foo()) # kaboom

    so, if you wanted your main at the top of the script:

    def main(args):

    … reference things in the global scope …

    return 0

    … more def’s and other things that main refers to …

    sys.exit(main(sys.argv))

  3. admin Says:

    Definitely true. I come from a sysadmin background and for short things I tend to do python is overkill. But for really big things I do indeed use python. Django especially rocks my coding world.

    Also, putting things into a main() I think can help organization quite a bit. A reminder of old C days, perhaps. It says “Here’s the main logic” to me.

    And yes, it was ranty and the read-only/write-only thing was meant to be exactly as serious as you took it, CBT. Plus, I just thought it sounded good/funny. :)

Leave a Comment