Skip to Content
Perl To Python Migration
book

Perl To Python Migration

by Martin C. Brown
November 2001
Beginner
320 pages
5h 53m
English
Pearson Business
Content preview from Perl To Python Migration

BASIC FUNCTION DEFINITION

Functions in Perl are defined using the sub statement:

sub FUNC
{
}

The equivalent in Python is the def statement:

def FUNC():
    # Statements

Note that the parentheses in the Python statement are compulsory.

Arguments are passed to a function in Perl as part of the @_ array. For example, the add() function can be defined like this:

sub add
{
    my ($x, $y) = @_;
    my $result = $x+$y;
    return $result;
}

I've deliberately made this function definition quite long so as to help when comparing it against the Python version.

Within Python, the function definition defines the arguments that the function accepts. For example, we can rewrite the above statement as:

def add(x,y):
    result = x+y
    return result

The x and y arguments ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl in a Nutshell

Perl in a Nutshell

Nathan Patwardhan, Ellen Siever, Stephen Spainhour
Perl by Example

Perl by Example

Ellie Quigley
Perl Hacks

Perl Hacks

Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe

Publisher Resources

ISBN: 0201734885Purchase book