Python vs. Perl

Dan bounces at foo.org
Thu Dec 16 00:31:56 EST 2004


I'm inexperienced in both languages, and am toying around with
both now, so I offer these comments with warnings of the blind
leading the blind.

As far as regular expressions go I can't offer much information.
They both meet my needs.  I prefer the Python syntax, however:
it is possible in both languages to compile a regex once and use
it many times, but it is more plainly specified in Python.

One difference I've noticed between the two languages is variable
scope.  By default, Perl variables are global, but there is a "my"
keyword that makes scope behave much as in C:

     #!/usr/bin/env perl
     my $foo = "globally defined \$foo";
     sub func_2 {
         print "func_2: $foo\n";
     }
     sub func_1 {
         my $foo = "func_1 defined \$foo";
         print "func_1: $foo\n";
         if (1) {
             my $foo = "if_block defined \$foo";
             print "if_block: $foo\n";
         }
         print "func_1: $foo\n";
         func_2()
     }
     print "main: $foo\n";
     func_1();
     func_2();
     print "main: $foo\n";

Here's the output.  Note that the assignment within the if_block
only holds within that block (it would have held to the end of
func_1() if not for the "my" keyword).  Note that the call of
func_2() from within func_1() does _not_ see the effect of the
assignment in func_1().

     main: globally defined $foo
     func_1: func_1 defined $foo
     if_block: if_block defined $foo
     func_1: func_1 defined $foo
     func_2: globally defined $foo
     func_2: globally defined $foo
     main: globally defined $foo

In Python, unless I just haven't figured it out yet, you can't
quite get this C-like scoping.  You can have global variables,
and variables local to functions, but I don't see any way to get
a variable local to an if block.

There is another keyword in Perl called "local" that makes
variables behave much like those of Emacs Lisp.  Take this
program for example:

     #!/usr/bin/env perl
     $foo = "globally defined \$foo";
     sub func_2 {
         print "func_2: $foo\n";
     }
     sub func_1 {
         local $foo = "func_1 defined \$foo";
         print "func_1: $foo\n";
         func_2()
     }
     print "main: $foo\n";
     func_1();
     func_2();
     print "main: $foo\n";

Here's the output:

     main: globally defined $foo
     func_1: func_1 defined $foo
     func_2: func_1 defined $foo
     func_2: globally defined $foo
     main: globally defined $foo

Notice that the first call of func_2() from func_1() _does_ see
the "local" assignment in func_1(), but that once func_1()
returns the global definition of $foo is restored and is now in
effect when func_2() is called from main.  I don't believe
Python has anything like this, but I don't know Python well
enough to be sure.  I can't think of how this would be useful in
Perl or Python, but it seems useful in Emacs Lisp.

Another difference between the languages is the amount of error
checking done at compile time.  Perl seems to catch more errors
at compile time.

You asked if Python is missing any features of Perl.  Perl has a
"tainted" mode that guards against insecure use of data provided
by users.  I don't know that Python has any equivalent.  I've
not used this, and I don't know how robust it's considered.

Another advantage of Perl is wider availability.  Until about a
year and a half ago, my primary environment was VMS.  And the
newest version of Python I could find for VMS was 1.4 when the
latest Python was 2.2.2.  Perl's well supported on VMS, and is
embeddable in VMS DCL command scripts.

But, for me, each language has one big advantage over the other.
Perl is the scripting language almost universally used in my
workplace.  There are a lot of existing scripts and lots of
local expertise in Perl.  I don't know of any existing Python
scripts, or Python users, in my building.  That's Perl's
strength in my world.  On the other hand, I find Perl's syntax
really hard to deal with.  I'm not a programmer, I'm an engineer
who infrequently needs to write a small program.  So, I write
something, and then find that I need to modify it or write
something new six months later.  I invariably forget everything
I know about Perl in six months.  I've been "learning" Perl
since 1998, but find I keep reverting to C (or even Emacs Lisp!)
for small, simple programs because I can't remember how to do it
in Perl.  So, I thought I'd give Python a try.  The syntax
certainly seems a lot cleaner and easier to remember.  We'll see
how it goes.

/Dan

-- 
dedded att verizon dott net



More information about the Python-list mailing list