[Python-il] Perl Vs. Python on Various Points

Shlomi Fish shlomif at iglu.org.il
Mon Jul 13 12:57:50 IDT 2009


On Monday 13 July 2009 12:00:23 Tal Einat wrote:
> Hi Shlomi,
>
> I've used Perl much more than I wanted to, and Python extensively by
> choice.
>
> > 1. Syntax as an Indicative of What the Language is Doing:
> > ---------------------------------------------------------
> > I said I happen to like it because the extra characters convey meaning.
>
> Perl has very few built-in types; I recall numbers, strings, arrays,
> hashes and functions. When you need to use anything else you are
> forced to use references, which are indistinguishable from numbers and
> require "casting" their targets' values back to their actual type. 

References are not numbers. They are different types of scalars. You can tell 
them apart using perldoc -f ref . You may be able to add a reference to a 
number, without a lot of meaning, or perform other arithmetic operations, but 
it's not encouraged.

Regarding numbers and strings - Perl does not distinguish between the two:

<<<<<<<<<<<
shlomi:~$ re.pl
$ "20" + "4"
24
$ 200.75 . 52
200.7552
$ shlomi:~$
>>>>>>>>>>

Internally, Perl does can store numbers and strings differently, but it 
seemlessly convert between the two.

Aside from strings and numbers a simple scalar can hold the special value 
"undef()" which is similar to NULL in C in that it indicates nothing. One can 
use perldoc -f defined to test for defined-ness. 

> You
> are forced to use references for various other reasons as well (e.g.
> when using hashes). My code was littered with '$', '@', '#' and '&'
> symbols, many of which were required just to work with references, and
> I found that this created more confusion than clarity.
>

Perl distinguishes between the array @array and the scalar $array. Now $array 
can hold a reference to an array, and the array pointed by it will be @$array. 
Normally, one can dereference complex data structures using ->:

$array_ref->[5]

Or:

$hash_ref->{"MyKey"}[5]{"AnotherKey"}

Python has references too, except that there every variable is a reference.

The distinction between references and non-references lists, allows some neat 
tricks such as:

my @total_array = map { @$_ } @array; # Concatenate a list

Or:

# Create a hash that one can use to determine if a key exists in a list.

my %exists_map = (map { $_ => 1 } @keys); 

> > 2. Comparison Operators:
> > ------------------------
> >
> > Now python only has "==" and friends for comparison (at least as far
> > as I know) while Perl 5 has both ==/!=/>/etc. and eq/ne/gt/etc. The
> > first ones are intended for numeric comparison and the latter ones for
> > string comparison.
>
> First off, Python has the "is" operator which tells you whether two
> variables refer to the same object. 

Ah, I see.

> The "==" operator tells you
> whether the objects referred to by two variables are considered equal,
> with the meaning of being equal defined by the type of the relevant
> objects. Python takes the opinion that such "deep" comparison is
> usually what a programmer needs, and from my experience this is true.
>

OK.

> Numbers and strings aren't the only two basic useful data types in
> programming. When using Perl one has to look up specific functions
> used for comparison of various other types of objects, even for basic
> types such as arrays and hashes. 

Well, in Perl 5 you can overload cmp or <=> or both (I believe the DateTime 
module on CPAN does that). Most module authors write the comparison method as 
->cmp() or ->compare().

> In Python every class defines its own
> comparison methods (if relevant), which Python uses when evaluating
> "==" and friends. I can use "==" and ">=" and ">" to compare various
> types of containers, such as lists, sets, tuples and dicts, and these
> comparisons are simple and obvious. Besides making the code clean and
> readable, things like this make learning Python less tedious.
>

Can I also overload the meaning of these for my own containers? This may cause 
unexpected results. This is also the case for Perl with overloading.

> > 3. Circular References:
>
> I would be interested to know what kind of support Perl has for deep
> comparison of containers with circular references.

I'll investigate. That's not a feature in the core Perl language, but rather 
some higher-level routines provided by some CPAN modules.

>
> > 4. Hiding Code
>
> Usually you just don't want someone to have your original version of
> the code, so they don't copy/paste and develop their own product based
> on your code. 

Well, Richard M. Stallman and other free software zealots will disagree with 
you on this. ;-) Even Python itself is distributed along with the original 
version of the code, so other people can copy/paste and develop their own 
product based on the code. Its licence even permits changing the licence of 
the code, including making it proprietary.


> For this purpose obfuscation and/or compiling to
> byte-code are enough.
>

You may be right. However, I was under the impression that my conversation 
partner wanted even better protection.

> (Obligatory: Writing in Perl is all the obfuscation one would ever need ;)
>

That's an old joke and was never particularly funny or true. I can normally 
tell a lot about most Perl code by reading it. See for example:

http://www.shlomifish.org/humour/fortunes/shlomif.html#scary-perl-expertise

Naturally, one can obfuscate/golf/etc. Perl code, and many people enjoy such 
diversions, but I haven't seen any such code used for production or on the 
CPAN. You can also obfuscate code in C and other languages, yet they don't 
have the reputation that Perl has for being obfuscated by default.

>
> Thanks for sharing!
>

My pleasure.

Regards,

	Shlomi Fish


> - Tal
> _______________________________________________
> Python-il mailing list
> Python-il at hamakor.org.il
> http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
What Makes Software Apps High Quality -  http://xrl.us/bkeuk

God gave us two eyes and ten fingers so we will type five times as much as we
read.


More information about the Python-il mailing list