sobering observation, python vs. perl

Ben Bacarisse ben.usenet at bsb.me.uk
Thu Mar 17 16:47:19 EDT 2016


Marko Rauhamaa <marko at pacujo.net> writes:

> "Charles T. Smith" <cts.private.yahoo at gmail.com>:
>
>> Actually, I saw a study some years ago that concluded that python
>> could be both slower and faster than perl, but that perl had much less
>> deviation than python. I took that and accepted it, but was surprised
>> now that in exactly the field of application that I've traditionally
>> used perl, it really is better, er... faster.
>>
>> Furthermore, the really nice thing about python is its OO, but I've
>> really neglected looking into that with perl's OO capabilities.
>
> I haven't had such log processing needs as you, nor has it come down to
> performance in such a way. Do use the best tool for the job.
>
> (When it comes to freely formatted logs, gleaning information from them
> is somewhat of a lost cause. I've done my best to move to rigorously
> formatted logs that are much more amenable to post processing.)
>
> Perl might be strong on its home turf, but I am a minimalist and
> reductionist -- Perl was intentionally designed to be a maximalist,
> imitating the principles of natural languages. Python has concise,
> crystal-clear semantics that are convenient to work with.
>
> Compare Perl (<URL: http://www.perlmonks.org/?node_id=98357>):
>
>    my $str = "I have a dream";
>    my $find = "have";
>    my $replace = "had";
>    $find = quotemeta $find; # escape regex metachars if present
>    $str =~ s/$find/$replace/g;
>    print $str;
>
> with Python:
>
>    print("I have a dream".replace("have", "had"))

If you know the strings are "have" and "had", you can just write

  print 'I have a dream' =~ s/have/had/r;

but I think your point is to show up the lack of a string (rather than
regex) replace in Perl, so the strings should be considered arbitrarily
"dangerous".  For that purpose it might have been better to give the
example as

  print("I have a dream".replace(find, replace))

for which the closest Perl match is probably

  print 'I have a dream' =~ s/\Q$find/$replace/r;

The closest to the actual line -- where you can just edit two strings
with your only concern being the end quote of the string -- would be
something like

  my $find = 'have'; print 'I have a dream' =~ s{\Q$find}'had'r

I don't want to start a language war!  I'm not saying that this is as
simple and clear as the Python, but a "compare X with Y" should try to
do the best by both X and Y.

-- 
Ben.



More information about the Python-list mailing list