<<

Python for perl

Fernando Pineda 140.636 Similaries between Perl & Python

• Like perl, python is: – A ‘scripng’ language but also a general purpose language. – Interpreted – Comments start with ‘#’ – Mulple programming paradigms are supported • imperave • object-based • funconal – Dynamically typed – Large package repositories (CPAN, PyPI) Packages & repositories

• Comprehensive Perl • Python Package Index Archive Network (CPAN) (PyPI) • ~175K packages in CPAN • ~90K packages in PyPI • • easy_install • cpanm • • use • import Differences between perl & python

• Python (like and ) is strongly typed • Python code blocks delimited via indentaon rather than code blocks • Built in interacve-shell (perl has e.g. Devel::REPL) • In python EVERYTHING is an object Built-in Types Perl Python

• int,long • scalar • float,complex • array • string* • hash • list • tuple* • • frozenset* * immutable python objects • dict hps://docs.python.org/2/library/types.html • ...etc. Code blocks

perl python foreach my $x in (1..9) { for x in range (1,10) print “$x\n”; print(x) } versions, programs & scripts perl python script perl --version python --version

#!/usr/bin/env perl #!/usr/bin/env python

... perl statements ... ‘’’ python statements ... raw input()

#!/usr/bin/env python

# raw)input() returns a string object age = raw_input("What is your age? ")

# print() prints the string print (“your age: “+ age )

# type() returns a type ‘type’ # print() prints the type print(type(age)) Anonymous funcons

perl python my $f= sub { f = lambda x: my $x = shift; return 2*x return 2*$x; } print(f(2))

print $func_ref->(2) my $f= sub { 2*shift} f= lambda x: 2*x print $func->(2) print (f(2)) In python everything is an object

• type()shows the type of an object • dir() shows the methods in an object

type(“hello”)

dir(“hello”)

“hello”.__add__(“ world”)

type(9/8)

print(9/8) Creang your own objects perl python Package MyClass; class MyClass: def __init__(self): sub new { self.x = None return bless{x=>undef} }

$obj = MyClass->new(); obj = MyClass perl with Moo Package MyClass; use Moo; has x => {is => ‘rw’} Map perl (map)

my @items= 1..9; @squared = map {$_**2} @items python (map) items= range(1,10) squared = list(map(lambda x: x**2, items)) python (list comprehension)

list= range(1,10) squared = [x**2 for x in list] Reduce perl (reduce)

use List::Util (‘reduce’); my @items= 1..9; my $sum= reduce { $a + $ } @items; python (reduce) from functions import reduce items= range(1,10) sum = reduce(lambda x,y: x+y, items)) Trouble with versions

• Python 2.x vs Python 3.x – v.3 released in Dec. 2008 – Not backward compable with Python 2.x – Many projects sll in Python 2.x – PyPI has 25,948 v.5 packages ported to v.6(out of 90839) • Perl 5.x vs Perl 6.x – v.6 released in Dec. 2015 (announced in 2000) – Not backward compable with Perl 5.x – CPAN has 173,63 v.5 packages but does not yet support v.6 packages Raonalizing language issues

• Prepend to Python 2.x modules

from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals ...etc.

print 8/7 # with and with ‘division module’ • Prepend to Perl 2.x scripts and modules

use Modern::Perl; use Moo; # simplifies object creation/usage Addional material

• hps://docs.python.org • hps://pypi.python.org • hp://modernperlbooks.com/books/ modern_perl_2016