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