(slightly OT): Python and linux - very cool

Mart van de Wege mvdwege.usenet at drebbelstraat20.dyndns.org
Thu Aug 8 13:14:38 EDT 2002

On Thu, 08 Aug 2002 07:26:53 +0200, TuxTrax wrote:

> Hi Mart
> having just taken a look in my python book at the section (albeit
> briefly) on the regular expression module (import re), this was of
> interest to me. My book says it's a relatively recent module, designed
> to address some shortcomings in pythons handling of regular
> expressions.
> Does this module make the shortcomings you are discussing a non-issue,
> or do you feel there is more to be done to bring python up to the
> regex handling capabilities of perl?

IMO, not quite. Regexps are such an integral part of Perl that you can
just use bare regexps in your code without having to explicitly compile
them first. That does serve to make badly coded Perl look *absolutely*
awful, but it is also a great benefit.

For example, this is what I recently did to extract all IPs from my

----- BEGIN SCRIPT -----

use warnings; # Make Perl picky about syntax.
use strict; # Make Perl *really* picky.

my @iplist; # Declare an array to hold all IP addresses.

open (FILE, '/var/log/apache/access.log');

while (<FILE>) {
	next unless $1; # Skip if the first field is somehow empty.
	next if $1 eq ''; # Skip localhost.
	push @iplist, $1;
# @iplist now holds all IPs in the first field of access.log.

----- END SCRIPT -----

Python can do this too of course, but somehow this is the sort of task
that comes naturally to me in Perl. Note the use of the regexp:

1. I don't have to explicitly declare and compile it.
2. It operates on the default input variable ($_), so I don't have to
specify its target, I just use a bare regexp.

This is what I like about Perl. YMMV, but when I want to parse some files,
I grab Perl. For other things I may prefer Python, and for some things I
don't care.

Also not that this is a quick and dirty solution. For one, it doesn't toss
out duplicates, and for another, I probably could have compacted it more.


"Time expands and then contracts
When you're spinning in the grip of someone
Who is not an ordinary girl"
	Counting Crows - Hard Candy

More information about the Python-list mailing list