Perl has a mechanism that lets you intercept calls to undefined subroutines.
You just have to write a subroutine called AUTOLOAD, which will be called instead of the missing subroutine. This feature can be used in various ways. You can for example create a customised error message. Or you can implement subroutines that are loaded on demand: the first time they are called AUTOLOAD creates and installs the subroutine, so that subsequent calls will work the same way as if the subroutine had existed all along (this is probably how this mechanism got its name). I started using it to call PL/SQL stored procedures from Perl.
I have a lot of stored procedures that all look like
function do_something ( some_parameter varchar2, another_parameter integer)
return integer;
I can call this procedure using
DBI
my $sql = $conn->prepare(q{
begin
:r := do_something(:some_parameter, :another_parameter);
end;
});
my $result;
$sql->bind_param_inout(':r', \$result, 10);
$sql->bind_param(':some_parameter', $some);
$sql->bind_param(':some_parameter', $another);
$sql->execute();
It is quite easy to make a generic version of that code, which looks at the function name and the parameters it is given to generate the SQL statement
and bind the parameters. Using this as AUTOLOAD in the database access module, I do not have to write any more code (SQL or Perl) to access these stored procedures, and can just do
my $result = Database::do_something(
some_parameter=>'blah', another_parameter => 123);
If I add another stored procedure do_something_else, it will automatically become available to the Perl program,
which speeds up development quite a bit and makes the application code smaller and more maintainable by reducing redundancies.
my $result = Database::do_something_else(funny_parameter => 12345);