Never use die in generate code when throwing an error.
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Array.pm
1 package Moose::Meta::Method::Accessor::Native::Array;
2
3 use strict;
4 use warnings;
5
6 use B;
7 use Scalar::Util qw( looks_like_number );
8
9 our $VERSION = '1.13';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Moose::Meta::Method::Accessor::Native';
14
15 sub _inline_curried_arguments {
16     my $self = shift;
17
18     return q{} unless @{ $self->curried_arguments };
19
20     return 'unshift @_, @curried;'
21 }
22
23 sub _inline_check_argument_count {
24     my $self = shift;
25
26     my $code = q{};
27
28     if ( my $min = $self->_minimum_arguments ) {
29         my $err_msg = sprintf(
30             q{"Cannot call %s without at least %s argument%s"},
31             $self->delegate_to_method,
32             $min,
33             ( $min == 1 ? q{} : 's' )
34         );
35
36         $code
37             .= "\n"
38             . $self->_inline_throw_error($err_msg)
39             . " unless \@_ >= $min;";
40     }
41
42     if ( defined( my $max = $self->_maximum_arguments ) ) {
43         my $err_msg = sprintf(
44             q{"Cannot call %s with %s argument%s"},
45             $self->delegate_to_method,
46             ( $max ? "more than $max" : 'any' ),
47             ( $max == 1 ? q{} : 's' )
48         );
49
50         $code
51             .= "\n"
52             . $self->_inline_throw_error($err_msg)
53             . " if \@_ > $max;";
54     }
55
56     return $code;
57 }
58
59 sub _minimum_arguments { 0 }
60 sub _maximum_arguments { undef }
61
62 sub _inline_check_arguments { q{} }
63
64 sub _inline_check_var_is_valid_index {
65     my ( $self, $var ) = @_;
66
67     return $self->_inline_throw_error( q{'The index passed to }
68             . $self->delegate_to_method
69             . q{ must be an integer'} )
70         . qq{ unless defined $var && $var =~ /^-?\\d+\$/;};
71 }
72
73 1;