File Coverage

blib/lib/HTML/StickyForm/RequestHash.pm
Criterion Covered Total %
statement 22 22 100.0
branch 9 10 90.0
condition 4 5 80.0
subroutine 5 5 100.0
pod 2 2 100.0
total 42 44 95.4


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             HTML::StickyForm::RequestHash - Minimal CGI-like request object
5              
6             =head1 SYNOPSIS
7              
8             my $req=HTML::StickyForm::RequestHash->new(
9             key1 => 'abc',
10             key1 => 'def',
11             key2 => ['ghi','jkl'],
12             );
13             my @keys=$req->param;
14             my $val1=$req->param('key1');
15             my @val2=$req->param('key2');
16              
17             =head1 DESCRIPTION
18              
19             This class provides the minimum features required of a request object by
20             L, for use in cases where a normal request is not available.
21             This might be because an empty request is needed, or where parameters are
22             available, but it is not appropriate to use L or L.
23              
24             =cut
25              
26             package HTML::StickyForm::RequestHash;
27             BEGIN {
28 4     4   17779 $HTML::StickyForm::RequestHash::VERSION = '0.07_02';
29             }
30              
31 4     4   17 use strict;
  4         6  
  4         67  
32 4     4   11 use warnings;
  4         4  
  4         743  
33              
34             =head1 CLASS METHODS
35              
36             =over
37              
38             =item new(PAIRLIST)
39              
40             Constructor. Creates a request object with the supplied list of parameters.
41             Multiple values for the same parameter name can be set up either by passing
42             multiple name/value pairs, or by passing arrayrefs for values. It is not an
43             error to mix these methods - all supplied values will be set in the new object.
44              
45             =cut
46              
47             sub new{
48 7     7 1 1304 my $class=shift;
49 7         5 my %self;
50              
51 7         31 while(my($name,$val)=splice @_,0,2){
52 11   100     27 my $array=$self{$name}||=[];
53 11 100 66     31 if($val && ref($val) eq 'ARRAY'){
54 3         9 push @$array,@$val;
55             }else{
56 8         15 push @$array,$val;
57             }
58             }
59              
60 7         24 bless \%self,$class;
61             }
62              
63             =back
64              
65             =head1 METHODS
66              
67             =over
68              
69             =item param()
70              
71             Returns a list of the names of all configured parameters. Each name is listed
72             only once, regardless of how many values are configured for any given name.
73              
74             =item param($name)
75              
76             In scalar context, returns the first configured value for the given name.
77             In list context, returns all configured values for the given name.
78              
79             =cut
80              
81             sub param{
82 17     17 1 2137 my($self)=shift;
83 17 100       32 if(my($name)=@_){
84 10 50       18 $name='' unless defined $name;
85 10 100       24 my $array=$self->{$name}
86             or return;
87 8 100       18 return @$array if wantarray;
88 4         13 return $array->[0];
89             }else{
90 7         32 return keys %$self;
91             }
92             }
93              
94             =back
95              
96             =cut
97              
98             # Return true to require
99             1;
100              
101              
102             =head1 AUTHOR
103              
104             Copyright (C) Institute of Physics Publishing 2005-2011
105              
106             Peter Haworth
107              
108             You may use and distribute this module according to the same terms
109             that Perl is distributed under.
110