ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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:
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.
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";
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
- 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)
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.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.