summaryrefslogtreecommitdiffstats
path: root/lib/Fun.pm
blob: abd62be57bd5c7897a8dbfede508dace028808ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package Fun;
use strict;
use warnings;
# ABSTRACT: simple function signatures

use Devel::CallParser;
use XSLoader;

XSLoader::load(
    __PACKAGE__,
    exists $Fun::{VERSION} ? ${ $Fun::{VERSION} } : (),
);

use Exporter 'import';
our @EXPORT = our @EXPORT_OK = ('fun');

=head1 SYNOPSIS

  use Fun;

  fun float_eq ($a, $b, $e = 0.0001) {
      return abs($a - $b) < $e;
  }

=head1 DESCRIPTION

This mdoule provides C<fun>, a new keyword which defines functions the same way
that C<sub> does, except allowing for function signatures. These signatures
support defaults and slurpy arguments, but no other advanced features. The
behavior should be equivalent to taking the signature, stripping out the
defaults, and injecting C<< my <sig> = @_ >> at the start of the function, and
then applying defaults as appropriate.

=cut

=head1 EXPORTS

=head2 fun

Behaves identically to C<sub>, except that it does not support prototypes or
attributes, but it does support a simple function signature. This signature
consists of a comma separated list of variables, each variable optionally
followed by C<=> and an expression to use for a default. For instance:

  fun foo ($x, $y = 5) {
      ...
  }

Defaults are evaluated every time the function is called, so global variable
access and things of that sort should work correctly.

C<fun> supports creating both named and anonymous functions, just as C<sub>
does.

=cut

sub fun {
    my ($code) = @_;
    return $code;
}

=head1 BUGS

No known bugs.

Please report any bugs through RT: email
C<bug-fun at rt.cpan.org>, or browse to
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Fun>.

=head1 SEE ALSO

L<signatures>, etc...

=head1 SUPPORT

You can find this documentation for this module with the perldoc command.

    perldoc Fun

You can also look for information at:

=over 4

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Fun>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Fun>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Fun>

=item * Search CPAN

L<http://search.cpan.org/dist/Fun>

=back

=cut

1;