File: Data.pm

package info (click to toggle)
libformvalidator-simple-perl 0.29-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 412 kB
  • sloc: perl: 3,043; makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,242 bytes parent folder | download | duplicates (7)
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
package FormValidator::Simple::Data;
use strict;
use Scalar::Util;
use FormValidator::Simple::Exception;
use FormValidator::Simple::Constants;

sub new {
    my $class = shift;
    my $self  = bless { }, $class;
    $self->_init(@_);
    return $self;
}

sub _init {
    my ($self, $input) = @_;
    $self->{_records} = {};
    my $errmsg = qq/Set input data as a hashref or object that has the method 'param()'./;
    if ( Scalar::Util::blessed($input) ) {
        unless ( $input->can('param') ) {
            FormValidator::Simple::Exception->throw($errmsg);
        }
        foreach my $key ( $input->param ) {
            my @v = $input->param($key);
            $self->{_records}{$key} = scalar(@v) > 1 ? \@v : $v[0];
        }
    }
    elsif ( ref $input eq 'HASH' ) {
        $self->{_records} = $input;
    }
    else {
        FormValidator::Simple::Exception->throw($errmsg);
    }
}

sub has_key {
    my ($self, $key) = @_;
    return exists $self->{_records}{$key} ? TRUE : FALSE;
}

sub param {
    my ($self, $keys) = @_;
    my @values = map {
        exists $self->{_records}{$_}
             ? $self->{_records}{$_}
             : ''
             ;
    } @$keys;
    return wantarray ? @values : \@values;
}

1;
__END__