X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FFunction%2FParameters.pm;h=5402a4d9d5e449f0722a7ccf4eb8ee438e151c2e;hb=ac1545feb0a2ec83e01edfd49268220c5e523227;hp=f0a9f7bdb004d3028cf8bb0e5a0b45cc55b30497;hpb=b72eb6ee4e3b6553e62365de04c6271ac8e180e5;p=p5sagit%2FFunction-Parameters.git diff --git a/lib/Function/Parameters.pm b/lib/Function/Parameters.pm index f0a9f7b..5402a4d 100644 --- a/lib/Function/Parameters.pm +++ b/lib/Function/Parameters.pm @@ -29,7 +29,16 @@ sub _assert_valid_attributes { my @bare_arms = qw(function method); my %type_map = ( function => { name => 'optional' }, - method => { name => 'optional', shift => '$self' }, + method => { + name => 'optional', + shift => '$self', + attrs => ':method', + }, + classmethod => { + name => 'optional', + shift => '$class', + attrs => ':method', + }, ); sub import { @@ -52,29 +61,38 @@ sub import { ? $proto : [$proto, $bare_arms[$bare++] || confess(qq{Don't know what to do with "$proto"})] ; - my ($name, $type) = @$item; + my ($name, $proto_type) = @$item; _assert_valid_identifier $name; - unless (ref $type) { - # use '||' instead of 'or' to preserve $type in the error message - $type = $type_map{$type} - || confess qq["$type" doesn't look like a valid type (one of ${\join ', ', sort keys %type_map})]; + unless (ref $proto_type) { + # use '||' instead of 'or' to preserve $proto_type in the error message + $proto_type = $type_map{$proto_type} + || confess qq["$proto_type" doesn't look like a valid type (one of ${\join ', ', sort keys %type_map})]; } - $type->{name} ||= 'optional'; - $type->{name} =~ /^(?:optional|required|prohibited)\z/ - or confess qq["$type->{name}" doesn't look like a valid name attribute (one of optional, required, prohibited)]; - $type->{shift} and _assert_valid_identifier $type->{shift}, 1; - $type->{attrs} and _assert_valid_attributes $type->{attrs}; + my %type = %$proto_type; + my %clean; + + $clean{name} = delete $type{name} || 'optional'; + $clean{name} =~ /^(?:optional|required|prohibited)\z/ + or confess qq["$clean{name}" doesn't look like a valid name attribute (one of optional, required, prohibited)]; + + $clean{shift} = delete $type{shift} || ''; + _assert_valid_identifier $clean{shift}, 1 if $clean{shift}; + + $clean{attrs} = delete $type{attrs} || ''; + _assert_valid_attributes $clean{attrs} if $clean{attrs}; - $spec{$name} = $type; + %type and confess "Invalid keyword property: @{[keys %type]}"; + + $spec{$name} = \%clean; } for my $kw (keys %spec) { my $type = $spec{$kw}; - $^H{HINTK_SHIFT_ . $kw} = $type->{shift} || ''; - $^H{HINTK_ATTRS_ . $kw} = $type->{attrs} || ''; + $^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 : @@ -176,14 +194,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 and C (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, C, or a hash reference (see -below). The difference between C and C is that Cs -automatically L their first argument into C<$self>. +values have to be either C, C, C or a hash +reference (see below). The difference between C and C is that +Cs automatically L their first argument into +C<$self> (Cs are similar but shift into C<$class>). The following shortcuts are available: @@ -252,9 +271,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