When Assembly Leaks Through

| 1 Comment

Paraphrasing Piers Cawley talking about why to give up Ruby for Perl (paraphrased):

This is assembly language for calling a function:

    push address of RETURN on stack
    push $argument on stack
    get address of FUNC
    goto FUNC
RETURN:
    pop return value off of stack

This is Perl 5 for calling a function:

func($argument);

That's all well and good. This is assembly language for entering a function:

    pop parameter from stack
    verify parameter type constraints
    ...

This is Perl 5 for entering a function:

sub func
{
    my $parameter = shift;
    die 'Wrong type' unless $parameter->isa( 'Whatever' );
    ...
}

... but wouldn't it be nice if:

sub func(Whatever $parameter) { ... }

(No new insight here, but directly prior to this talk, Damian Conway and I talked about how Perl 6 signatures simplify much, much more code than this simple example illustrates. Lots of code goes away.

1 Comment

Aye. I make the same point in a Perl 6 advent blog post about parameters in Perl 6.

My example is slightly longer, but it too probably understates how much boilerplate code, and mental effort, is saved by not doing parameter handling manually.

Modern Perl: The Book

cover image for Modern Perl: the book

The best Perl Programmers read Modern Perl: The Book.

sponsored by the How to Make a Smoothie guide

Categories

Pages

About this Entry

This page contains a single entry by chromatic published on June 21, 2010 10:11 AM.

Assumptions was the previous entry in this blog.

The Virtuous Dilemma of Iterative Improvements is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.


Powered by the Perl programming language

what is programming?