Functional Programming in Perl: Part 1

As we know  functional programming involves creating and manipulating functions to build up larger programs. This requires a language that allows functions to be used as input and return data to other functions. Perl has two important features that make it possible: –

* Code references

* Closures

Lets take code reference first:-

Need of code reference:

Sometimes we need to manipulate a subroutine by reference so we need to take the references to functions. This might happen if you need to create a signal handler, a Tk callback, or a hash of function pointers etc.

To get a code reference:

$cref = &func;

Reference to anonymous functions

$cref = sub { ... };

To call a code reference:

Using a postfix arrow notation for dereferencing a code reference.

@returned = $cref->(@arguments);

A way to call the subroutine by reference prior to Perl 5.004

@returned = &$cref(@arguments);


If the name of a function is func, you can produce a reference to this code by preceding that name with & . You can also create anonymous functions using the sub {} notation. These code references can be stored just like any other reference. So we can say that code references are same as function pointers in C and C++ and which certainly helps to improve coding.

In my next post I will try to cover closures.

Thanks for reading.

Ref: Perl Cookbook by Tom Christiansen & Nathan Torkingston

ORM for existing projects using Phrasebook

Just few questions came in mind so I thought to get some expert advice. Well, I am not an DB expert but comfortable with SQL/Perl DBI and can write queries using Phrasebook. I mostly use PostgresSQL DB. As there is a lot of discussion of ORM and many new implementations are coming using ORM techniques (e.g.; DBIx::Class etc) for DB interaction so just wondering how can I use ORM in my existing projects as I am still using DBI + Phrasebook and what benefit shall I get if I switch to ORM?

Thanks for reading!