File Coverage

blib/lib/Boulder/Util.pm
Criterion Covered Total %
statement 15 62 24.1
branch 0 32 0.0
condition 0 3 0.0
subroutine 5 8 62.5
pod 2 3 66.6
total 22 108 20.3


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