NAME

Function::Parameters - subroutine definitions with parameter lists

SYNOPSIS

use Function::Parameters;

fun foo($bar, $baz) {
  return $bar + $baz;
}

fun mymap($fun, @args) :(&@) {
  my @res;
  for (@args) {
    push @res, $fun->($_);
  }
  @res
}

print "$_\n" for mymap { $_ * 2 } 1 .. 4;

method set_name($name) {
  $self->{name} = $name;
}
use Function::Parameters 'proc', 'meth';

my $f = proc ($x) { $x * 2 };
meth get_age() {
  return $self->{age};
}

DESCRIPTION

This module lets you use parameter lists in your subroutines. Thanks to Devel::Declare it works without source filters.

WARNING: This is my first attempt at using Devel::Declare and I have almost no experience with perl's internals. So while this module might appear to work, it could also conceivably make your programs segfault. Consider this module alpha quality.

Basic stuff

To use this new functionality, you have to use fun instead of sub - sub continues to work as before. The syntax is almost the same as for sub, but after the subroutine name (or directly after fun if you're writing an anonymous sub) you can write a parameter list in parentheses. This list consists of comma-separated variables.

The effect of fun foo($bar, $baz) { is as if you'd written sub foo { my ($bar, $baz) = @_; , i.e. the parameter list is simply copied into my and initialized from @_.

In addition you can use method, which understands the same syntax as fun but automatically creates a $self variable for you. So by writing method foo($bar, $baz) { you get the same effect as sub foo { my $self = shift; my ($bar, $baz) = @_; .

Customizing the generated keywords

You can customize the names of the keywords injected in your package. To do that you pass a hash reference in the import list:

use Function::Parameters { proc => 'function', meth => 'method' }; # -or-
use Function::Parameters { proc => 'function' }; # -or-
use Function::Parameters { meth => 'method' };

The first line creates two keywords, proc and meth (for defining functions and methods, respectively). The last two lines only create one keyword. Generally the hash keys can be any identifiers you want while the values have to be either function or method. The difference between function and method is that methods automatically shift their first argument into $self.

The following shortcuts are available:

use Function::Parameters;
   # is equivalent to #
use Function::Parameters { fun => 'function', method => 'method' };
use Function::Parameters 'foo';
  # is equivalent to #
use Function::Parameters { 'foo' => 'function' };
use Function::Parameters 'foo', 'bar';
  # is equivalent to #
use Function::Parameters { 'foo' => 'function', 'bar' => 'method' };

Other advanced stuff

Normally, Perl subroutines are not in scope in their own body, meaning the parser doesn't know the name foo or its prototype while processing sub foo ($) { foo $bar[1], $bar[0]; }, parsing it as $bar->foo([1], $bar[0]). Yes. You can add parens to change the interpretation of this code, but foo($bar[1], $bar[0]) will only trigger a foo() called too early to check prototype warning. This module attempts to fix all of this by adding a subroutine declaration before the definition, so the parser knows the name (and possibly prototype) while it processes the body. Thus fun foo($x) :($) { $x } really turns into sub foo ($); sub foo ($) { my ($x) = @_; $x }.

If you need subroutine attributes, you can put them after the parameter list with their usual syntax.

Syntactically, these new parameter lists live in the spot normally occupied by prototypes. However, you can include a prototype by specifying it as the first attribute (this is syntactically unambiguous because normal attributes have to start with a letter).

If you want to wrap Function::Parameters, you may find import_into helpful. It lets you specify a target package for the syntax magic, as in:

package Some::Wrapper;
use Function::Parameters ();
sub import {
  my $caller = caller;
  Function::Parameters::import_into $caller;
  # or Function::Parameters::import_into $caller, @other_import_args;
}

import_into is not exported by this module, so you have to use a fully qualified name to call it.

AUTHOR

Lukas Mai, <l.mai at web.de>

COPYRIGHT & LICENSE

Copyright 2010, 2011 Lukas Mai.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.