| 1 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | |||||||||||||||
| 4 | # ABSTRACT: Provides a function keyword | ||||||||||||||
| 5 | |||||||||||||||
| 6 | package Syntax::Feature::Function; | ||||||||||||||
| 7 | { | ||||||||||||||
| 8 | 1 | $Syntax::Feature::Function::VERSION = '0.002'; | |||||||||||||
| 9 | } | ||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | ▶ | use Carp qw( croak ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use Function::Parameters (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | ▶ | use B::Hooks::EndOfScope; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 1 | ▶ | use Import::Into; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 1 | $Carp::Internal{ +__PACKAGE__ }++; | |||||||||||||
| 19 | |||||||||||||||
| 20 | 2 | ▶ | sub install { | ||||||||||||
|
Subroutine
install: called CC=9 SCAR=24.0
Pod
documented
| |||||||||||||||
| 21 | 2 | my ($class, %args) = @_; | |||||||||||||
| 22 | |||||||||||||||
| 23 | 2 | my $target = $args{into}; | |||||||||||||
| 24 | 2 | my $options = $args{options}; | |||||||||||||
| 25 | 2 | my @names = qw( fun ); | |||||||||||||
| 26 | |||||||||||||||
| 27 | # we received options | ||||||||||||||
| 28 | 2 | ▶ | if (defined $options) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | 1 | my $options_ref = ref $options; | |||||||||||||
| 30 | |||||||||||||||
| 31 | # function => { ... } | ||||||||||||||
| 32 | 1 | ▶ | if ($options_ref eq 'HASH') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | # function => { -as => ... } | ||||||||||||||
| 35 | 1 | ▶ | if (defined( my $as = $options->{ -as } )) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 1 | my $as_ref = ref $as; | |||||||||||||
| 37 | |||||||||||||||
| 38 | # -as => 'fun' | ||||||||||||||
| 39 | 1 | ▶ | if (not $as_ref) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 40 | |||||||||||||||
| 41 | 1 | @names = ($as); | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | # -as => [qw( fun f )] | ||||||||||||||
| 45 | elsif (ref $as eq 'ARRAY') { | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 0 | @names = @$as; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | # bad -as type | ||||||||||||||
| 51 | else { | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 0 | croak q(The '-as' option to the 'function' syntax feature only accepts) | |||||||||||||
| 54 | . q( scalar and array reference values); | ||||||||||||||
| 55 | } | ||||||||||||||
| 56 | } | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | # bad options type | ||||||||||||||
| 60 | else { | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 0 | croak q(Options for feature must be a hash or array reference); | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | # make sure all names are valid | ||||||||||||||
| 67 | m/\A[_a-z][_a-z0-9]*\Z/i | ||||||||||||||
| 68 | or croak qq(Invalid function declaration identifier '$_') | ||||||||||||||
| 69 | 2 | ▶ | for @names; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | |||||||||||||||
| 71 | # install handlers | ||||||||||||||
| 72 | Function::Parameters->import::into($target, $_) | ||||||||||||||
| 73 | 2 | for @names; | |||||||||||||
| 74 | |||||||||||||||
| 75 | 1 | ▶ | on_scope_end { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 76 | 1 | namespace::clean->clean_subroutines($target, @names); | |||||||||||||
| 77 | 1 | }; | |||||||||||||
| 78 | |||||||||||||||
| 79 | 1 | return 1; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 1 | 1; | |||||||||||||
| 83 | |||||||||||||||
| 84 | __END__ | ||||||||||||||