add new files to MANIFEST
[p5sagit/Function-Parameters.git] / lib / Function / Parameters.pm
index 88c3e66..8af625e 100644 (file)
@@ -5,14 +5,14 @@ use v5.14.0;
 use strict;
 use warnings;
 
+use Carp qw(confess);
+
 use XSLoader;
 BEGIN {
-       our $VERSION = '0.05_03';
+       our $VERSION = '0.06';
        XSLoader::load;
 }
 
-use Carp qw(confess);
-
 sub _assert_valid_identifier {
        my ($name, $with_dollar) = @_;
        my $bonus = $with_dollar ? '\$' : '';
@@ -28,11 +28,24 @@ sub _assert_valid_attributes {
 
 my @bare_arms = qw(function method);
 my %type_map = (
-       function => { name => 'optional' },
+       function => {
+               name => 'optional',
+               default_arguments => 1,
+               check_argument_count => 0,
+       },
        method   => {
                name => 'optional',
+               default_arguments => 1,
+               check_argument_count => 0,
+               attrs => ':method',
                shift => '$self',
+       },
+       classmethod   => {
+               name => 'optional',
+               default_arguments => 1,
+               check_argument_count => 0,
                attrs => ':method',
+               shift => '$class',
        },
 );
 
@@ -78,6 +91,13 @@ sub import {
                $clean{attrs} = delete $type{attrs} || '';
                _assert_valid_attributes $clean{attrs} if $clean{attrs};
                
+               $clean{default_arguments} =
+                       exists $type{default_arguments}
+                       ? !!delete $type{default_arguments}
+                       : 1
+               ;
+               $clean{check_argument_count} = !!delete $type{check_argument_count};
+
                %type and confess "Invalid keyword property: @{[keys %type]}";
 
                $spec{$name} = \%clean;
@@ -86,13 +106,16 @@ sub import {
        for my $kw (keys %spec) {
                my $type = $spec{$kw};
 
+               my $flags =
+                       $type->{name} eq 'prohibited' ? FLAG_ANON_OK :
+                       $type->{name} eq 'required' ? FLAG_NAME_OK :
+                       FLAG_ANON_OK | FLAG_NAME_OK
+               ;
+               $flags |= FLAG_DEFAULT_ARGS if $type->{default_arguments};
+               $flags |= FLAG_CHECK_NARGS if $type->{check_argument_count};
+               $^H{HINTK_FLAGS_ . $kw} = $flags;
                $^H{HINTK_SHIFT_ . $kw} = $type->{shift};
                $^H{HINTK_ATTRS_ . $kw} = $type->{attrs};
-               $^H{HINTK_NAME_ . $kw} =
-                       $type->{name} eq 'prohibited' ? FLAG_NAME_PROHIBITED :
-                       $type->{name} eq 'required' ? FLAG_NAME_REQUIRED :
-                       FLAG_NAME_OPTIONAL
-               ;
                $^H{+HINTK_KEYWORDS} .= "$kw ";
        }
 }
@@ -115,6 +138,8 @@ sub unimport {
 
 __END__
 
+=encoding UTF-8
+
 =head1 NAME
 
 Function::Parameters - subroutine definitions with parameter lists
@@ -189,14 +214,15 @@ 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' };
+ use Function::Parameters { meth => 'method' }; # etc.
 
 The first line creates two keywords, C<proc> and C<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 C<function>, C<method>, or a hash reference (see
-below). The difference between C<function> and C<method> is that C<method>s
-automatically L<shift|perlfunc/shift> their first argument into C<$self>.
+values have to be either C<function>, C<method>, C<classmethod> or a hash
+reference (see below). The difference between C<function> and C<method> is that
+C<method>s automatically L<shift|perlfunc/shift> their first argument into
+C<$self> (C<classmethod>s are similar but shift into C<$class>).
 
 The following shortcuts are available:
 
@@ -265,9 +291,11 @@ turns into
 
 =back
 
-Plain C<'function'> is equivalent to C<< { name => 'optional' } >>, and plain
+Plain C<'function'> is equivalent to C<< { name => 'optional' } >>, plain
 C<'method'> is equivalent to
-C<< { name => 'optional', shift => '$self', attrs => ':method' } >>.
+C<< { name => 'optional', shift => '$self', attrs => ':method' } >>, and plain
+C<'classmethod'> is equivalent to
+C<< { name => 'optional', shift => '$class', attrs => ':method' } >>.
 
 =head2 Syntax and generated code