File: ValidateXS.pm

package info (click to toggle)
libparams-validate-perl 0.91-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 524 kB
  • ctags: 729
  • sloc: perl: 2,596; makefile: 52
file content (160 lines) | stat: -rw-r--r-- 2,901 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package Params::Validate;

use strict;
use warnings;

require XSLoader;
XSLoader::load( 'Params::Validate', $Params::Validate::VERSION );


my $default_fail = sub { require Carp;
                         Carp::confess($_[0]) };

{
    my %defaults = ( ignore_case   => 0,
		     strip_leading => 0,
		     allow_extra   => 0,
		     on_fail       => $default_fail,
		     stack_skip    => 1,
                     normalize_keys => undef,
		   );

    *set_options = \&validation_options;
    sub validation_options
    {
	my %opts = @_;

	my $caller = caller;

	foreach ( keys %defaults )
	{
	    $opts{$_} = $defaults{$_} unless exists $opts{$_};
	}

	$OPTIONS{$caller} = \%opts;
    }
}

sub _check_regex_from_xs { return ( defined $_[0] ? $_[0] : '' ) =~ /$_[1]/ ? 1 : 0 }

BEGIN
{
    if ( $] >= 5.006 && $] < 5.007 )
    {
        eval <<'EOF';
sub check_for_error
{
    if ( defined $Params::Validate::ERROR )
    {
        $Params::Validate::ON_FAIL ||= sub { require Carp; Carp::croak( $_[0] ) };

        $Params::Validate::ON_FAIL->($Params::Validate::ERROR)
    }
}

sub validate_pos (\@@)
{
    local $Params::Validate::ERROR;
    local $Params::Validate::ON_FAIL;
    local $Params::Validate::CALLER = caller;

    my $r;
    if (defined wantarray)
    {
        $r = &_validate_pos;
    }
    else
    {
        &_validate_pos;
    }

    check_for_error();

    return wantarray ? @$r : $r if defined wantarray;
}

sub validate (\@$)
{
    local $Params::Validate::ERROR;
    local $Params::Validate::ON_FAIL;
    local $Params::Validate::CALLER = caller;

    my $r;
    if (defined wantarray)
    {
        $r = &_validate;
    }
    else
    {
        &_validate;
    }

    check_for_error();

    return wantarray ? %$r : $r if defined wantarray;
}

sub validate_with
{
    local $Params::Validate::ERROR;
    local $Params::Validate::ON_FAIL;
    local $Params::Validate::CALLER = caller;

    my $r;
    if (defined wantarray)
    {
        $r = &_validate_with;
    }
    else
    {
        &_validate_with;
    }

    check_for_error();

    my %p = @_;
    if ( UNIVERSAL::isa( $p{spec}, 'ARRAY' ) )
    {
        return wantarray ? @$r : $r if defined wantarray;
    }
    else
    {
        return wantarray ? %$r : $r if defined wantarray;
    }
}
EOF

        die $@ if $@;
    }
    else
    {
        *validate      = \&_validate;
        *validate_pos  = \&_validate_pos;
        *validate_with = \&_validate_with;
    }
}

1;

__END__

=head1 NAME

Params::ValidateXS - XS implementation of Params::Validate

=head1 SYNOPSIS

  See Params::Validate

=head1 DESCRIPTION

This is an XS implementation of Params::Validate.  See the
Params::Validate documentation for details.

=head1 COPYRIGHT

Copyright (c) 2004-2007 David Rolsky.  All rights reserved.  This
program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut