LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-22-2013, 01:28 AM   #1
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 557

Rep: Reputation: 46
Perl or Python


Hello Everybody!


Though I would love to use Perl in automation and other scripting tasks, I become a faint-hearted (I admit it) when I have to write Perl CGI for handling Form Data. I am happy, if I can use PHP in order to handle Form Data. Where does Python fit with respect to doing things such as RegEx, Pattern Matching, etc. which Perl does just fine and in a superb way and handling form data and handing them over from one web page to another without having us get tired of writing codes (PHP seems better here)?

Thanks in advance for your views!

P.S.: One of my Senior Managers has suggested me to go for Python as I am already having good exposures to Perl.
 
Old 02-22-2013, 01:45 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,929

Rep: Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306
regex and pattern matching are more or less on the same level, just the syntax differs.
both have a lot of libraries, and a huge amount of info available on the web. So probably python is better (in general), but you need to learn both of them to be able to use them professionally.
 
Old 02-22-2013, 10:00 AM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 11,290
Blog Entries: 3

Rep: Reputation: 1446Reputation: 1446Reputation: 1446Reputation: 1446Reputation: 1446Reputation: 1446Reputation: 1446Reputation: 1446Reputation: 1446Reputation: 1446
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 02-22-2013, 10:21 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
To the OP.

Compare https://en.wikipedia.org/wiki/Anonymous_function#Python to https://en.wikipedia.org/wiki/Anonymous_function#Perl_5 - no limitations in Perl, limitations in Python.

Python started supporting closures only from Python 3, which, AFAIK, is not as popular as Python 2. Anyway, since closures in Python is an afterthought, the syntax is ugly.

AFAIK Python does not have lexical scoping - see https://en.wikipedia.org/wiki/Scope_...ter_science%29 .

Taking all this Python is an underlanguage for me.

Whenever I'm looking into a new language, I first of all check presence of the above mentioned features.

So you exposure to Perl might be insufficient.

Python might be easier WRT writing "C" bindings for it, but with Inline::C Perl module it's not a problem for Perl too.
 
Old 02-22-2013, 11:32 AM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 393

Rep: Reputation: 157Reputation: 157
Quote:
Originally Posted by Sergei Steshenko View Post
AFAIK Python does not have lexical scoping - see https://en.wikipedia.org/wiki/Scope_...ter_science%29.
Python has only lexical scoping. What it does not have is dynamic scoping, which Perl has by the use of local, but I don't think that's an argument either way – dynamic scoping is “evil”.

Some more differences:
- Python has nicer object model than Perl.
Some syntax differences which I notice the most:
- I've grown to love how I can omit parenthesis in Perl in many cases.
- I hate indention dictating blocks in Python.
- I find $, @ and % too much of a hassle to type.

Last edited by mina86; 02-22-2013 at 11:36 AM.
 
Old 02-22-2013, 11:43 AM   #6
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,771

Rep: Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468
Quote:
Originally Posted by devUnix View Post
I have to write Perl CGI for handling Form Data. I am happy, if I can use PHP in order to handle Form Data. Where does Python fit with respect to doing things such as RegEx, Pattern Matching, etc. which Perl does just fine and in a superb way and handling form data and handing them over from one web page to another without having us get tired of writing codes (PHP seems better here)?
There's not even a question. Python with a web framework like Django or Flask. Like PHP, but superior in every way.

Here's how Python handles regular expressions and pattern matching:

http://docs.python.org/howto/regex.html
 
Old 02-22-2013, 11:50 AM   #7
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 248

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Sergei Steshenko View Post
Python started supporting closures only from Python 3, which, AFAIK, is not as popular as Python 2.
Not true, Python 2 does support closures, just in a limited way, which was solved by introducing nonlocal statement in Python 3 and which I guess you're talking about. But here's an example of closure that works in Python 2:
Code:
def pow(x):
    def fn(y):
        return y ** x
    return fn

pow2 = pow(2)
pow3 = pow(3)

print pow2(10)
print pow3(10)
To OP: while Perl supports regular expressions as part of the language, in Python they're provided as functions by re module from standard library. AFAIK, Perl's regexps have better Unicode support, can't tell you anything more specific about it as I know very little Perl. Regarding web apps, if I were you, I wouldn't bother with CGI at all and use some small web framework instead. For Python I like Flask, for Perl there's Mojolicious and Dancer.
 
Old 02-22-2013, 05:23 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by mina86 View Post
Python has only lexical scoping. What it does not have is dynamic scoping, which Perl has by the use of local, but I don't think that's an argument either way – dynamic scoping is “evil”.

Some more differences:
- Python has nicer object model than Perl.
Some syntax differences which I notice the most:
- I've grown to love how I can omit parenthesis in Perl in many cases.
- I hate indention dictating blocks in Python.
- I find $, @ and % too much of a hassle to type.
Perl has both dynamic (historically first) and lexical scoping. Nowadays (i.e. since Perl 5 - 1994 ?) mostly lexical scoping is used


If you are saying Python has lexical scoping, translate into Python the following:

Code:
sergei@amdam2:~/junk> cat -n ./lexical_scoping.pl
     1  #!/usr/bin/perl
     2
     3  use strict;
     4  use warnings;
     5
     6  my $x = 0;
     7  {
     8  my $x = $x + 1;
     9    {
    10    my $x = $x + 1;
    11      {
    12      my $x = $x + 1;
    13        {
    14        my $x = $x + 1;
    15          {
    16          my $x = $x + 1;
    17          warn "\$x=$x";
    18          }
    19        warn "\$x=$x";
    20        }
    21      warn "\$x=$x";
    22      }
    23    warn "\$x=$x";
    24    }
    25  warn "\$x=$x";
    26  }
    27  warn "\$x=$x";
sergei@amdam2:~/junk> ./lexical_scoping.pl
$x=5 at ./lexical_scoping.pl line 17.
$x=4 at ./lexical_scoping.pl line 19.
$x=3 at ./lexical_scoping.pl line 21.
$x=2 at ./lexical_scoping.pl line 23.
$x=1 at ./lexical_scoping.pl line 25.
$x=0 at ./lexical_scoping.pl line 27.
sergei@amdam2:~/junk>

Quote:
I've grown to love how I can omit parenthesis in Perl in many cases
- once stumbled upon a program adding some kind of parenthesis to Python code top facilitate code refactoring. If you think about it, code without clearly visible block boundaries is very fragile in case of refactoring.

Quote:
I find $, @ and % too much of a hassle to type
- and I find supporting somebody else's code with no indication what a name means too much of a a hassle to support. Furthermore, because of lack of '$', '@' Python formatted output sucks - rewrite in Python


Code:
my $n = 10;
my $f = 1.23;
my $s = "a string";

print "integer: $n; float: $f; string: $s\n";
 
Old 02-23-2013, 06:16 AM   #9
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 248

Rep: Reputation: 107Reputation: 107
That's block scoping, which Python doesn't have, but it doesn't mean that Python's scoping is not lexical. Here's a code snippet in Python (lexical scoping):
Code:
x = 1

def foo():
    global x
    print x
    x = 2

def bar():
    x = 3
    foo()

bar()    # Prints 1
print x  # Prints 2
And here's identical snippet in bash (dynamic scoping):
Code:
x=1

foo() {
  echo $x
  x=2
}

bar() {
  local x=3
  foo
}

bar      # Prints 3
echo $x  # Prints 1
Quote:
Originally Posted by Sergei Steshenko View Post

- and I find supporting somebody else's code with no indication what a name means too much of a a hassle to support. Furthermore, because of lack of '$', '@' Python formatted output sucks - rewrite in Python


Code:
my $n = 10;
my $f = 1.23;
my $s = "a string";

print "integer: $n; float: $f; string: $s\n";
What does "Python formatted output sucks" mean?
Code:
n = 10
f = 1.23
s = 'a string'

print 'integer: %d; float: %.2f; string: %s' % (n, f, s)
# Or using .format() method.
print 'integer: {}; float: {:.2f}; string: {}'.format(n, f, s)
 
Old 02-23-2013, 06:31 AM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by audriusk View Post
That's block scoping, which Python doesn't have, but it doesn't mean that Python's scoping is not lexical. Here's a code snippet in Python (lexical scoping):
Code:
x = 1

def foo():
    global x
    print x
    x = 2

def bar():
    x = 3
    foo()

bar()    # Prints 1
print x  # Prints 2
And here's identical snippet in bash (dynamic scoping):
Code:
x=1

foo() {
  echo $x
  x=2
}

bar() {
  local x=3
  foo
}

bar      # Prints 3
echo $x  # Prints 1


What does "Python formatted output sucks" mean?
Code:
n = 10
f = 1.23
s = 'a string'

print 'integer: %d; float: %.2f; string: %s' % (n, f, s)
# Or using .format() method.
print 'integer: {}; float: {:.2f}; string: {}'.format(n, f, s)
Sucks means that you need to split the string and use all those % format specifiers.

Regarding lexical scoping - I suggest to read https://en.wikipedia.org/wiki/Scope_...exical_scoping .
 
Old 02-23-2013, 07:13 AM   #11
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 248

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Sergei Steshenko View Post
Sucks means that you need to split the string and use all those % format specifiers.

Regarding lexical scoping - I suggest to read https://en.wikipedia.org/wiki/Scope_...exical_scoping .
OK, I guess it comes down to personal preference, because I don't mind Python's way of interpolating strings.

I did read the link you gave, it was quite interesting and useful read, thanks for that. Actually, I took bash example in my previous post from Dynamic Scoping part and rewrote it in Python. If you're trying to tell that Python's scoping is not lexical, I disagree. My given Python and bash examples clearly behave differently, even though they're mostly identical. Also the fact that Python does have closures ("lexical closures" in full, which by definition capture their lexical scope) should take away any doubts. Your Perl example demonstrates that code blocks create their own scope, in Python only functions do that. In fact, the Wikipedia article does mention this (emphasis mine):
Quote:
Many languages take function scope slightly further, allowing variables to be made local to just part of a function; rather than having the entire function as its scope, a variable might have block scope, meaning that it's scoped to just a single block of statements.

<...> languages with block scope typically also allow the use of "naked" blocks, which frequently serve no other purpose than to allow fine-grained control of variable scope.
 
Old 02-23-2013, 07:34 AM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by audriusk View Post
OK, I guess it comes down to personal preference, because I don't mind Python's way of interpolating strings.

I did read the link you gave, it was quite interesting and useful read, thanks for that. Actually, I took bash example in my previous post from Dynamic Scoping part and rewrote it in Python. If you're trying to tell that Python's scoping is not lexical, I disagree. My given Python and bash examples clearly behave differently, even though they're mostly identical. Also the fact that Python does have closures ("lexical closures" in full, which by definition capture their lexical scope) should take away any doubts. Your Perl example demonstrates that code blocks create their own scope, in Python only functions do that. In fact, the Wikipedia article does mention this (emphasis mine):
AFAIR, in practice (in year 2000) Python had just two levels of scope - top level and inside a function.
 
Old 02-23-2013, 08:06 AM   #13
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 248

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Sergei Steshenko View Post
AFAIR, in practice (in year 2000) Python had just two levels of scope - top level and inside a function.
I vaguely remembered something from then (I got introduced to Python around that time), so googled up and found this:
Quote:
In Python 2.1, statically nested scopes were added as an optional feature, to be enabled by a from __future__ import nested_scopes directive. In 2.2 nested scopes no longer need to be specially enabled, and are now always present.
So you're right: up till version 2.1 there was no nested scoping in Python.
 
Old 02-23-2013, 11:12 AM   #14
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,771

Rep: Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468Reputation: 1468
Thumbs up

Quote:
Originally Posted by Sergei Steshenko View Post
Sucks means that you need to split the string and use all those % format specifiers.
You don't. The last line of post #9 is how it's done these days.

Last edited by dugan; 02-23-2013 at 11:14 AM.
 
Old 02-23-2013, 07:25 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by dugan View Post
You don't. The last line of post #9 is how it's done these days.
If you mean this:

Code:
print 'integer: {}; float: {:.2f}; string: {}'.format(n, f, s)
line, it still sucks, but for different reasons.

1) usage of '{...}' - which are items not present in the rest of the code. I mean in Perl for, say, scalar variable I use '$' - be it in '$x = 1;' or in '" ... $x ...";
2) usage of .format(n, f, s).

On the latter. In Perl I write simply as I think:

Code:
print "integer: $n; float: $f; string: $s\n";
.

I mean that the variables appear in the output string in the places they are supposed to be expanded. Nothing is specified in addition.

In case of Python:

Code:
print 'integer: {}; float: {:.2f}; string: {}'.format(n, f, s)
I have two headaches:

a) I need to specify formats in the string;
b) I need to specify variables in 'format(n, f, s)' in correct order - this is inherited from "C" *printf functions. I.e. it sucks in "C" too.


Overall, monitoring Python evolution is funny. Originally it was conceived as under-Perl, i.e. some Perl features were intentionally dropped. And now Python 3 catches up with Perl introducing features Perl already head in 1994.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python related: How to access a Perl script behind a firewall from Python? vxc69 Programming 8 12-14-2010 07:32 AM
Perl Vs python knockout_artist Linux - Newbie 6 10-15-2008 11:06 AM
My needs: Perl vs. Python dave201 Programming 25 08-11-2007 11:13 PM
perl vs python yenonn Programming 6 08-01-2006 05:44 AM
Perl or Python ! linuxlover1 Programming 13 04-19-2004 07:33 AM


All times are GMT -5. The time now is 11:40 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration