File Coverage

blib/lib/Boulder/Simple.pm
Criterion Covered Total %
statement 9 60 15.0
branch 0 32 0.0
condition 0 3 0.0
subroutine 3 8 37.5
pod 4 5 80.0
total 16 108 14.8


line stmt bran cond sub pod time code
1             # Copyright (c) 2004 Timothy Appnel
2             # http://www.timaoutloud.org/
3             # This code is released under the Artistic License.
4             #
5             # Boulder::Simple - a class for simple Boulder IO interaction.
6             # DEPRECATED. Use Boulder::Util instead.
7             #
8              
9             package Boulder::Simple;
10              
11 1     1   719 use strict;
  1         2  
  1         41  
12 1     1   1660 use CGI::Util qw( unescape escape );
  1         5786  
  1         112  
13              
14 1     1   10 use vars qw( $VERSION );
  1         6  
  1         7470  
15             $VERSION = '0.021';
16              
17             sub save {
18 0     0 1   my($class,$filehandle,$data) = @_;
19 0           $filehandle = to_filehandle($filehandle);
20 0           local($,) = '';
21 0           local($\) = '';
22 0 0         $data = [ $data ] if (ref($data) eq 'HASH');
23 0           foreach my $rec (@$data) {
24 0           my $param;
25 0           foreach $param (keys %$rec) {
26 0           my($escaped_param) = escape($param);
27 0           my @vals = ref($rec->{$param}) eq 'ARRAY' ?
28 0 0         @{$rec->{$param}} : ( $rec->{$param} );
29 0           my $v;
30 0           foreach $v (@vals) {
31 0           print $filehandle "$escaped_param=",escape("$v"),"\n";
32             }
33             }
34 0           print $filehandle "=\n";
35             }
36             }
37              
38             sub load_as_query {
39 0     0 1   my @lines = load(@_);
40 0 0         return undef unless @lines;
41 0 0         "@lines" =~ /=/ ? join("&",@lines) : join("+",@lines);
42             }
43              
44             sub load_as_hash {
45 0     0 1   my @lines = load(@_);
46 0 0         return undef unless @lines;
47 0           my %hash;
48 0           foreach (@lines) {
49 0           my($key,$value)=split /=/, $_, 2;
50 0 0         next unless $key;
51 0           $value = unescape($value);
52 0 0         unless (exists($hash{$key})) {
53 0           $hash{$key} = $value;
54 0           next;
55             }
56 0 0         if (ref($hash{$key}) eq 'ARRAY') {
57 0           push(@{$hash{$key}},$value);
  0            
58             } else {
59 0           $hash{$key} = [ $hash{$key}, $value ];
60             }
61             }
62 0           \%hash;
63             }
64              
65             sub load {
66 0     0 1   my($class,$filehandle) = @_;
67 0           my @lines;
68 0 0 0       if (defined($filehandle) && ($filehandle ne '')) {
69 0           while (<$filehandle>) {
70 0           chomp;
71 0 0         last if /^=/;
72 0           push(@lines,$_);
73             }
74             }
75 0           @lines;
76             }
77              
78             # Borrowed from CGI so we don't have to load that package if
79             # we don't need to. Turns a string into a filehandle.
80             sub to_filehandle {
81 0     0 0   my $thingy = shift;
82 0 0         return undef unless $thingy;
83 0 0         return $thingy if UNIVERSAL::isa($thingy,'GLOB');
84 0 0         return $thingy if UNIVERSAL::isa($thingy,'FileHandle');
85 0 0         if (!ref($thingy)) {
86 0           my $caller = 1;
87 0           while (my $package = caller($caller++)) {
88 0 0         my($tmp) = $thingy=~/[\':]/ ? $thingy : "$package\:\:$thingy";
89 0 0         return $tmp if defined(fileno($tmp));
90             }
91             }
92 0           return undef;
93             }
94              
95             1;
96              
97             __END__