[Tutor] Equivalent to perl -e

Tim Peters tim.peters at gmail.com
Mon Oct 16 04:40:30 CEST 2006

[Chris Lasher]
> My professor and advisor has been "inspired" by me to give Python a
> try. He's an avid Perl user, and challenged me with the following:
> What is the Python equivalent to perl -e '<some oneliner>'?

The initally attractive but unsatisfying answer is:

    python -c '<some oneliner>'

The reason it's "unsatisfying" is that Python isn't concerned with making:

    <some oneliner>

pleasant, or even sanely possible, for many tasks.  Perl excels at
one-liners; Python doesn't much care about them.

> Embarassingly, I had no answer, but I figure, someone on the list will
> know. His use of Python is at stake; he threatened that, since he's
> dependant enough on using perl -e within Emacs enough, if it can't be
> done in Python, he won't take the language seriously. Help me, Python
> Tutor, you're his only hope!

Like many Python (very) old-timers, I used Perl heavily at the time
Python came out.  As was also true for many of them, as time went on
the size of a new program I was willing to write in Perl instead of in
Python got smaller and smaller, eventually reaching "almost 0".  I
still use Perl some 15 years later, but now /only/ for "perl -e"-style
1-liners at an interactive shell.  If it takes more than a line, I
stick it in a module (and maybe a class) for reuse later.

Python's strengths are more in readability, helpful uniformity, easy
use of classes and rich data structures, and maintainability.  Cryptic
one-liners are in general (but not always) opposed to all of those.

So, ya, "python -c" exists, but your professor won't be happy with it.
 That's fine!  If one-liners are all he cares about, Perl is usually
the best tool for the job.

More information about the Tutor mailing list