initial revision
[Method-Signatures-Simple.git] / lib / Method / Signatures / Simple.pm
bloba0016735c83895bd25ea92ea897a9d076f418e8b
1 package Method::Signatures::Simple;
3 use warnings;
4 use strict;
6 our $VERSION = '0.01_01';
8 use base q/Devel::Declare::MethodInstaller::Simple/;
10 sub import {
11 my $class = shift;
12 my %opts = @_;
13 $opts{into} ||= caller;
14 $opts{invocant} ||= '$self';
16 $class->install_methodhandler(
17 name => 'method',
18 %opts,
22 sub parse_proto {
23 my $self = shift;
24 my ($proto) = @_;
25 $proto ||= '';
26 $proto =~ s/[\r\n]//g;
27 my $invocant = $self->{invocant};
29 $invocant = $1 if $proto =~ s{^(\$\w+):\s*}{};
31 my $inject = "my ${invocant} = shift;";
32 $inject .= "my ($proto) = \@_;" if defined $proto and length $proto;
34 return $inject;
38 =head1 NAME
40 Method::Signatures::Simple - Basic method declarations with signatures, without source filters
42 =head1 VERSION
44 Version 0.01
46 =head1 SYNOPSIS
48 use Method::Signatures::Simple;
50 method foo { $self->bar }
52 # with signature
53 method foo($bar, %opts) {
54 $self->bar(reverse $bar) if $opts{rev};
57 # attributes
58 method foo : lvalue { $self->{foo} }
60 # change invocant name
61 method foo ($class: $bar) { $class->bar($bar) }
63 =head1 RATIONALE
65 This module provides a basic C<method> keyword with simple signatures. It's intentionally simple,
66 and is supposed to be a stepping stone for its bigger brothers L<MooseX::Method::Signatures> and L<Method::Signatures>.
67 It only has a small benefit over regular subs, so if you want more features, look at those modules.
68 But if you're looking for a small amount of syntactic sugar, this might just be enough.
70 =head1 FEATURES
72 =over 4
74 =item * invocant
76 The C<method> keyword automatically injects the annoying C<my $self = shift;> for you. You can rename
77 the invocant with the first argument, followed by a colon:
79 method ($this:) {}
80 method ($this: $that) {}
82 =item * signature
84 The signature C<($sig)> is transformed into C<"my ($sig) = \@_;">. That way, we mimic perl's usual
85 argument handling.
87 method foo ($bar, $baz, %opts) {
89 # becomes
91 sub foo {
92 my $self = shift;
93 my ($bar, $baz, %opts) = @_;
95 =back
98 =begin pod-coverage
100 =over 4
102 =item parse_proto
104 Overridden.
106 =back
108 =end pod-coverage
110 =head1 AUTHOR
112 Rhesa Rozendaal, C<< <rhesa at cpan.org> >>
114 =head1 BUGS
116 Please report any bugs or feature requests to C<bug-method-signatures-simple at rt.cpan.org>, or through
117 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Method-Signatures-Simple>. I will be notified, and then you'll
118 automatically be notified of progress on your bug as I make changes.
120 =head1 SUPPORT
122 You can find documentation for this module with the perldoc command.
124 perldoc Method::Signatures::Simple
127 You can also look for information at:
129 =over 4
131 =item * RT: CPAN's request tracker
133 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Method-Signatures-Simple>
135 =item * AnnoCPAN: Annotated CPAN documentation
137 L<http://annocpan.org/dist/Method-Signatures-Simple>
139 =item * CPAN Ratings
141 L<http://cpanratings.perl.org/d/Method-Signatures-Simple>
143 =item * Search CPAN
145 L<http://search.cpan.org/dist/Method-Signatures-Simple>
147 =back
149 =head1 ACKNOWLEDGEMENTS
151 =over 4
153 =item * MSTROUT
155 For writing L<Devel::Declare> and providing the core concepts.
157 =item * MSCHWERN
159 For writing L<Method::Signatures> and publishing about it. This is what got my attention.
161 =item * FLORA
163 For helping me abstracting the Devel::Declare bits and suggesting improvements.
165 =back
167 =head1 SEE ALSO
169 L<Devel::Declare>, L<Method::Signatures>, L<MooseX::Method::Signatures>.
171 =head1 COPYRIGHT & LICENSE
173 Copyright 2008 Rhesa Rozendaal, all rights reserved.
175 This program is free software; you can redistribute it and/or modify it
176 under the same terms as Perl itself.
179 =cut
181 1; # End of Method::Signatures::Simple