Softpanorama

May the source be with you, but remember the KISS principle ;-)
Contents Bulletin Scripting in shell and Perl Network troubleshooting History Humor

Scripting Bulletin, 2003

Prev Up Next

Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

[Nov 18, 2003] VBScript or Perl  by Robbie Allen

O'Reilly Network

The Windows Sys Admin's Scripting Dilemma  by Robbie Allen, coauthor of Active Directory, 2nd Edition, and author of the recently released Active Directory Cookbook

As I was working on Active Directory Cookbook, I had to make a decision that many Windows system administrators face at one point or another: whether to use VBScript or Perl? I'm a long-time Perl guy, but unfortunately, no matter how much I preach the benefits of Perl, the Windows sysadmin community hasn't embraced Perl the way it has been embraced on the Unix side. Ultimately I decided to use VBScript because it is the least common denominator as far as scripting languages go on the Windows platform (besides batch files, of course). I did, however, include Perl examples on my web site so as not to leave out the Perl coders.

I was able to take the easy way out by using both in this case, but most system admins only need one, but which one? I've already stated my preference towards Perl, but I want to outline the pros and cons of each because both languages have their place. You can accomplish most of the basic system admin tasks with either language. For this reason you'll need to look at the other advantages and disadvantages of each language to determine which works best for you.

Overview of VBScript

VBScript is a subset of the Visual Basic programming language. You can run VBScript code using a Windows Scripting Host interpreter (wscript.exe or cscript.exe) or from within Active Server Pages (ASP).

Advantages of VBScript

Disadvantages of VBScript

Overview of Perl on Windows

Perl has a long history dating back to 1987 when Larry Wall released the first version. Now, an army of dedicated Perl hackers actively maintain Perl, and ports exist for virtually every modern operating system.

Advantages of Perl

Disadvantages of Perl

Summary

If you want to get serious about automation in the Windows environment, I recommend using Perl because of its extensive module support and the overall robustness of the language. If you only want to do something quick and dirty or you don't have any programming experience, VBScript is probably your better bet.

As far as other scripting languages go, a case could be made for using JScript because of its integration with the .NET Framework, but I don't find many people using JScript for system admin tasks. I'll leave that for someone else to argue.

Getting Loopy with Python and Perl by Aahz

06/27/2002  | O'Reilly Network

This article is based in part on my O'Reilly Open Source Convention 2002 tutorial, "Python for [Perl] Programmers/" However, this article includes more Perl and Python comparison than I've included in the tutorial. My tutorial targets experienced programmers of all sorts, with the non-Python examples drawn from Perl. In this article I'll be comparing Python's loop constructs to Perl's.

Perl has two basic looping constructs: while and for/foreach. This doesn't count variations, such as "<statement> until EXPR" or do...while, nor the fact that for/foreach has two different forms. Python also has only two looping constructs: while and for. Unlike Perl, Python's loops have no variations; instead, the for loop uses a special protocol that generalizes well. Both Perl and Python have functional constructs that loop over a sequence, but that's outside the scope of this article.

The variation in available loop constructs exemplifies the basic difference between Perl and Python: Perl's motto is TMTOWTDI (There's More Than One Way To Do It), whereas Python's counter-motto is "There's Only One Way." Python's motto is the short form of one element of Python's design philosophy: "There should be one--and preferably only one--obvious way to do it." To see the rest of Python's design philosophy, start the Python interactive interpreter (Python 2.1.2 or later) and type "import this".

So how do Python's looping constructs actually work? Let's start with a basic Perl idiom:

    while (<STDIN>) {
        print;
    }

and compare it to the equivalent Python idiom:

    
    import sys
    for line in sys.stdin:
        sys.stdout.write(line)

The main thing to notice is that Python uses a for loop instead of a while loop. There are two reasons for this:

Prior to Python 2.2, the loop would have been written like this:

    import sys
    while 1:
        line = sys.stdin.readline()
        if not line:
            break
        sys.stdout.write(line)

In general, Python's for loop works much like Perl's for/foreach list form. In order to loop over a sequence of numbers, you need to produce a list:


    for i in range(10):
        print i

This will print the numbers from 0 through 9. Like Perl arrays, Python lists are zero-based, and the range() function caters to that. To prove that range() is in fact creating a list of numbers, fire up the Python interpreter:

    
    >>> range(10)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Contrast this with Perl's standard indexing loops:

    
    for ($i=0; $i<10; $i++) {
        print "$i\n";
    }

or

    
    foreach $i (0..9) {
        print "$i\n";
    }

Note particularly how range() specifies a value one higher than the maximum index; this makes it easy to use with the len() function. Generally speaking, range() is fast enough and consumes little enough memory that generating an entire list doesn't hurt. But if you're worried about that, Python does have the xrange() function that produces one number at a time.

Aside from the lack of assignment, Python's while loops function almost identically to their Perl counterparts:

Perl:

   
    $done = 0;
    while (!$done) {
        $input = getInput();
        if (defined($input)) {
            process($input);
        } else {
            $done = 1;
        }
    }

Python:


    done = False
    while not done:
        input = getInput()
        if input is not None:
            process(input)
        else:
            done = True

Note that False is a new, built-in value in Python 2.2.1 (in Python 2.3, boolean operations will return True/False instead of 1/0). In general, any empty value generates a false truth value: False, None, 0, "", (), [], {}, and class instances with __nonzero__() or __len__() methods that return 0.

Note also that in Perl, if getInput() starts returning a hash or array instead of a scalar, the while loop needs to be modified, whereas the Python loop will continue to work fine with a dict or a list. That's because in Python, everything is done with reference semantics (called "binding" in Python because one does not access references directly); one can get the same effect in Perl by explicitly using references.

In addition to iterators, Python 2.2 added generators. Generators are functions that return an iterator. It may help to think of them as something like resumable closures. Here's a subset implementation of grep:


    from __future__ import generators
    import sys
    import re

    def grep(seq, regex):
        regex = re.compile(regex)
        for line in seq:
            if regex.search(line):
                yield line

The "yield" keyword turns an ordinary function into a generator; the generator returns an iterator that wraps the generator. Each time the yield executes (returning a result just like "return" does), the generator is paused, but the generator function's stack frame is retained (including all local variables), pending another call to the iterator's next() method. The iterator does not execute any of the generator's code until the first call to next().

Returning to this specific example, calling grep() returns an iterator. Each call to the iterator's next() method returns one match. This happens implicitly in a for loop:


     regex = r"\s+\w+\s+"
     for line in grep(sys.stdin, regex):
         sys.stdout.write(line)

The advantage of all this is that it's simple, clean, and efficient. You can write straightforward code--but it doesn't need to hog memory by creating an entire list before returning. Generators can be pipelined; imagine recreating other Unix utilities, such as uniq. Even if the source is a list, at least there's no need to create temporary lists.

The "from __future__" statement is needed because "yield" is a new keyword in Python 2.2 and accessing it must be done explicitly. When Python 2.3 is released, "from __future__ import generators" will no longer be required, but keeping it won't harm anything (allowing code to run under any version Python 2.2 or later).

For more information about converting Perl code to Python, see the Python/Perl Phrasebook . (The phrasebook is seven years old and out of date, but still quite useful.)

Special thanks to Cathy Mullican (menolly@spy.net) for refreshing my badly outdated Perl knowledge.

[Aug 14, 2003] O'Reilly Network Guido van Rossum Speaks by Steve Holden

Guido van Rossum is the well-known creator of the Python programming language. During his address at OSCON 2003's States of the Unions event, he announced that he'd soon be leaving PythonLabs to work for a California startup. Guido graciously agreed to an interview with Steve Holden on the move, recent developments, and Python in general.

... ... ...

ORN:
Tim O'Reilly's keynote at OSCON was quite thought provoking. What do you think were his most interesting points?

GvR: I really liked his point about how the nature of the popular applications changes, and that Amazon or Google are applications just as much as Word or PowerPoint. I thought his point that those applications have a lot of data, and that they leverage the people who interact with the data, was particularly telling.

I think that was the key idea from his speech, and I really think that in the Python world there are things we can do with that idea. For example, the PyPI, the Python Package Index, might actually become much more useful if people can feed back what they think of a particular piece of software.

ORN: Do you think that the open source development model has proved itself as a viable alternative to proprietary methods? Can the cathedral coexist with the bazaar?

GvR: Open source development methods have absolutely proved themselves, and I don't think there's any immediate likelihood that the cathedral will be demolished. However, among open source projects, I'm a fan of those that are consciously run with a bazaar-style model.

The projects where there are a lot of programmers paid by a particular company, whether it's Netscape with Mozilla or Sun with Open Office, even though they claim a lot of success in terms of downloads, are extremely hard for the average programmer to make a contribution because the code base is so enormous, and the learning curve is therefore rather steep.

Projects that started out as grassroots, like Python, have developed more of a community and a process that makes it much more acceptable.

ORN: Python is a part of a larger open-source community. What do you see as the major challenges facing that community in the next couple of years?

GvR: Oh, man, I'm not that kind of visionary, to have an opinion about that. Some people are worried about a few things, like software patents, but in my view software patents are so absurd that I expect that open source will happily survive all that, even if situations arise where a few specific projects are forced to rewrite some of their code to work around a patent.

A few individuals will probably be hit by unfair lawsuits, but by and large I don't think that's going to break the open source community.

ORN: I sometimes think it's a good job nobody has patented breathing; otherwise we'd all owe them money.

GvR: I guess there was prior art among the reptiles [smiles].

ORN: How does Python differ from other open source projects?

GvR: I guess one difference is that it has a long history, since it was first distributed in 1991. In those days nobody talked about "open source", and Richard Stallman [founder of the Free Software Foundation] wasn't very well known and the GNU General Public License didn't exist.

It's amazing how many people who are still active in the Python community were at the first Python workshop in 1994.

ORN: How has the Python development community changed over the last few years?

GvR: The role of PythonLabs has actually been diminished, and although the perception is that PythonLabs still controls a large percentage of the core code, in fact the reality is that PythonLabs folks have all been hacking on various pieces of Zope and ZODB. So the larger developer community has taken over and has done so very successfully.

We've had a lot of "new kids" join as developers and become active in the community. We've also seen that the geographical base has broadened quite a lot, with contributions coming in from outside Western Europe and the United States.

ORN: Why do you think Python is less popular than Perl?

GvR: Perl was there first. It's definitely older than Python, and when I decided to create Python I looked at the existing Perl, which I think was 3.0. Even by that time it had managed to create a lot of mind share, and it definitely filled a need in the user community.

Perl, Python, Ruby and Tcl are the four dynamic programming languages that get the most publicity as open source projects, and I think they have a lot more in common than they differ.

ORN: What's being done (and by whom) to improve Python's visibility and grow the user community?

GvR: I don't know that enough is being done, but there are certainly a number of people who are very active in that area. I do it myself by staying where I am and giving keynotes at conferences and making my personal life the subject of discussions on Slashdot. I didn't elect to do that, but it happened--someone listening to my OSCON keynote posted the news of my move to Slashdot, and some people seemed to feel they had the right to comment on it.

ORN: Perhaps they should get lives of their own instead of discussing yours?

GvR: Perhaps. Anyway, there are some people taking specific actions to promote Python, notably Kevin Altis, who among other things put the OSCON Python track together and has set himself the goal of increasing the number of Python users tenfold in the next year or two.

There's now a "python-marketing" mailing list, and Stephan Deibel of Archaeopteryx is very active there, issuing press releases for the Python Software Foundation about various things. He'll be doing another one for the upcoming Python 2.3 release, where hopefully we can include a quote from Apple about the inclusion of Python 2.3 in the next MacOS X release.

ORN: How did you come to decide to set the 2.3 release date so the software could be included in the gold master for the next Mac OS release?

GvR: Well, Apple expressed interest, and the schedules almost matched already. Basically all we had to do was bring our deadline forward one week.

ORN: Have you seen a good response to this decision from the developer community?

GvR: Very good! Usually what happens is that when I get my act together and set a release date, all sorts of people who have been putting off checking in their changes and reviewing their patches suddenly become active. So we've seen a flurry of activity.

Of course there are a few people who are disappointed with the compressed schedule, because it's meant their changes have had to be rejected for 2.3, but there's always 2.4 or even 2.3.1.

ORN: How would you like to see the Python Software Foundation develop over, say, the next three years?

GvR: Observing how other similar non-profits like the Apache Software Foundation and also the Open Software Initiative, of which I'm a board member, I'm struck by how the PSF is still run by geeks, none of whom have significant experience running a non-profit. I think as a result we're not always doing the right things, and we don't always do things at the right time; we don't exactly know how to do it. So either we argue endlessly about how to do things, or we do them badly.

It would be good to invite some new people who are friendly to Python onto the board for the next year. You could say that I think the board needs a little adult supervision [smiles]. I realize that this is not something I'm good at: I head the board because people know my name, but I'm not very good at the schmoozing that it takes to get companies to sign up as a sponsor member, for example, or to attract large donations.

ORN: What's the situation with the python.org domain ownership, and do you anticipate any changes any time soon?

GvR: As you might know, CNRI still owns that domain, and has let the PSF use it without much restriction, although they did ask us to keep the 1.6 release on there despite the fact that nobody much uses it.

They have finally approached me, having found the time to address the question, with a contract to handle the transfer of the domain name, which is probably the most important thing, a number of trademarks, and the copyright to the content of the python.org website. The CNRI position has been that they own that copyright because the web site was started when we were all at CNRI.

That's actually quite an important contribution to the open source movement by CNRI.

ORN: What are the most difficult aspects of reconciling open source software development and making a living?

GvR: It gets more difficult once you have a family, because it's more important to have a steady stream of income that lets you pay your bills and put something away for when the kid goes to college. It seems a long way away, but you have to start saving almost from the moment they're born.

That limits me in the choice of employment.

ORN: What have been the major changes in patterns of Python usage lately? What do you see as the most interesting new application areas?

GvR: There are lots of interesting ways that Python is being applied, but none of them are especially new. Python has always been very broadly applied.

What I do appreciate is that Python is slowly but steadily becoming more known among at least the avant-garde teachers and educators. There are more and amore people who aren't too tied to traditional school districts and who are looking into Python and teaching it.

ORN: This resonates with your long-held interests in "computer programming for everyone". Don't you think that perhaps "everyone" is too broad, and that there aren't at least some people who will never be capable programming a computer?

GvR: That's a deep philosophical question. I'm optimistic about that in theory.

There are plenty of countries, although unfortunately the USA isn't one of them, where 100% of the population is literate. That doesn't necessarily mean that they can write novels or newspaper articles, but they can all read and write, and if you taught them how to type they could all express their thoughts in an email or a blog entry, for example.

Given that I believe everybody can learn to read and write, given the right education and circumstances--obviously if your parents have no money and you're sent to work when you're seven years old, you're not in a very good situation unless you're exceptionally smart--I believe that the same thing would be possible for programming and thinking logically to some extent.

ORN: What value do Python skills have in the job market? Why should a programmer bother to learn Python rather than Visual Basic or Java?

GvR: If you say, "Why should a programmer learn Python", I could say, "Because it's the most fun of all the available languages". There are situations where you're required to code everything for production in, say, Java, but you still have the opportunity to do prototypes or testing frameworks or personal and ad hoc programs using Python. So it often proves useful in situations where you least expected it. But if you don't know it, you won't be able to use it, so you may lose out on an opportunity to do something extra for your boss because you knew this new tool.

I don't think that at the moment there are enough places advertising jobs with Python as a requirement to say that Python will give you a bigger chance of getting a job, although there are some places where that's obviously true. For example, Industrial Light and Magic has seven hundred Python programmers!

... ... ...

ORN: Do you have any writing plans at present?

GvR: I don't. I'm going to put all my energy into Elemental Security and one day a week just running the Python community, sort of guiding where the next developments go. I would dearly love to write, but I can't even add something to my blog more than once every couple of months.

ORN: The literature has expanded hugely in the last three years. What gaps, if any, do you see in Python coverage nowadays?

GvR: I love the way that literature has grown. Is there a book on wxPython yet? That's still a gap [Patrick O'Brien has a book in the planning stages]. Apart from that I'd love to see more materials for use in classrooms for use by the "computer programming for everyone" crowd.

Those people are looking for tutorials and examples and books, and they have very different requirements; they can't use "Learning Python" or any of the other standard texts.

... ... ...

ORN: What's your single favorite Python application that someone else wrote?

GvR: That's an overwhelming choice; there are so many categories, and there are things that I know exist that I have never used myself that I think are absolutely exciting.

Maybe I should mention Adele Goldberg's and Dennis Allison's ThinkFive application that they presented here at OSCON. In half a year with two people they were able to create a content management system in Zope and Python, with a little bit of SmallTalk that allows master teachers to create really high-quality online classes in subject topics like geometry and algebra.

I was really very impressed by what they did, and there are three kinds of users who interact with the system. First, the people who create the courses, who interact with Zope and create Flash animations and all sorts of other stuff in XML. Then there are the teachers who use the system in the classroom, or at least mentor the students, who are taking the courses from home in some cases, and there are the students. ThinkFive does something for each of those groups.

ORN: You've spent a lot of time on Zope recently, and it's a product nearing a major new release. What is your favorite feature that people will see in Zope 3 (and why)?

GvR: That's hard to say, because everything in Zope 3 is different. What I like in Zope 3 is that for the Python programmer writing code to go into a web site, things are much less confusing than they were in Zope 2.

ORN: What do you feel was your biggest technical, professional or personal mistake along the trail from ABC to Python 2.3b2?

GvR: I think the biggest mistake was a personal one, my decision to join BeOpen.

ORN: What advice would you give to young people just starting their careers in information technology?

GvR: There is still a lot of fun to be had in software development and don't believe all the negative stuff about how the IT boom is over.

Computers still run a significant part of our lives and they will continue to get a lot better. So we will continue to have a lot of need for a lot of smart people to program the applications of the future that nobody's heard of or can even dream of.

Steve Holden acts as a consultant, advising clients on system and network architectures and the design and implementation of programmed web systems. He is also the author of "Python Web Programming".



Etc

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit exclusivly for research and educational purposes.   If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner. 

ABUSE: IPs or network segments from which we detect a stream of probes might be blocked for no less then 90 days. Multiple types of probes increase this period.  

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Haterís Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least


Copyright © 1996-2016 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License.

The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down you can use the at softpanorama.info

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: January, 24, 2014