| 1 | package Parse::Yapp::KeyValue; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | our $VERSION = 0.10; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Parse::Yapp::KeyValue - parser for simple key/value pairs | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 DESCRIPTION | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | parse a string of simple key/value pairs and | ||||||||||||||||||||
| 15 | store the results in a hash reference for easy | ||||||||||||||||||||
| 16 | access. Parse::KeyValue correctly handles | ||||||||||||||||||||
| 17 | escaped quotes as well as escaped backslashes. | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | Parse::KeyValue will parse the following example | ||||||||||||||||||||
| 20 | string: | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | AL=53 AK=54 AB=55 TN="home sweet home" =$ | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | into a hashref with the following contents: | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | { | ||||||||||||||||||||
| 27 | '' => '$', | ||||||||||||||||||||
| 28 | 'AL' => '53', | ||||||||||||||||||||
| 29 | 'TN' => 'home sweet home', | ||||||||||||||||||||
| 30 | 'AK' => '54', | ||||||||||||||||||||
| 31 | 'AB' => '55' | ||||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | multiple identical keys are treated as arrays. | ||||||||||||||||||||
| 35 | the string | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | A=1 A=2 A=3 | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | will return a hash reference with the following | ||||||||||||||||||||
| 40 | contents: | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | { A => [ 1, 2, 3 ] } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | tokens without an associated key name will be | ||||||||||||||||||||
| 45 | treated as pairs with an empty string for the | ||||||||||||||||||||
| 46 | key. the string | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | yeah alabama "crimson tide" | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | will return a hash reference with the following | ||||||||||||||||||||
| 51 | contents: | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | { '' => [ 'yeah', 'alabama', 'crimson tide' ] } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | my $str = 'A=1 K=2 B=3 A=4'; | ||||||||||||||||||||
| 58 | my $kv = new Parse::Yapp::KeyValue; | ||||||||||||||||||||
| 59 | my $href = $kv->parse($str); | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | print $href ? Dumper $href : "parse failed\n"; | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =head1 TODO | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | - configurable delimiter | ||||||||||||||||||||
| 66 | - flags to alter behavior in the event of | ||||||||||||||||||||
| 67 | multiple keys (error, overwrite last value, | ||||||||||||||||||||
| 68 | keep first value) | ||||||||||||||||||||
| 69 | - flag to require ALL values be inside an array | ||||||||||||||||||||
| 70 | reference, not just keys with multiple values | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 1 | ▶ | use Parse::Yapp::KeyValue::Parser; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 75 | 1 | ▶ | use Parse::Lex; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 1 | my @lex = | |||||||||||||||||||
| 78 | ( | ||||||||||||||||||||
| 79 | OP_ASSIGNMENT => '=', | ||||||||||||||||||||
| 80 | KEY => '[A-Za-z0-9]+', | ||||||||||||||||||||
| 81 | TEXT_Q_SINGLE => '\'[^\']+\'', | ||||||||||||||||||||
| 82 | TEXT_Q_DOUBLE => '"[^"]+"', | ||||||||||||||||||||
| 83 | TEXT_NONQUOTED => '[^\'" ]+', | ||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =head1 METHODS | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =over 4 | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =item new | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | instantiates a new Parse::Yapp::KeyValue object. | ||||||||||||||||||||
| 93 | no arguments are currently accepted. | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | sub new | ||||||||||||||||||||
| 98 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 99 | 1 | my $proto = shift; | |||||||||||||||||||
| 100 | 1 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 1 | return bless {}, $class; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =item parse | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | parses the supplied string and returns a hash | ||||||||||||||||||||
| 108 | reference containing the parsed data. in the | ||||||||||||||||||||
| 109 | even that parsing fails, undef is returned. | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | sub parse | ||||||||||||||||||||
| 114 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 115 | 1 | my $self = shift; | |||||||||||||||||||
| 116 | 1 | my $str = shift; | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | # handle escaped quotes and escaped backslashes | ||||||||||||||||||||
| 119 | # this is just wrong, but it's quick and easy | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 1 | $str =~ s/\\\\/\x01/g; | |||||||||||||||||||
| 122 | 1 | $str =~ s/\\'/\x02/g; | |||||||||||||||||||
| 123 | 1 | $str =~ s/\\"/\x03/g; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 1 | my $lexer = new Parse::Lex @lex; | |||||||||||||||||||
| 126 | 1 | $lexer->from($str); | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 1 | my $parser = Parse::Yapp::KeyValue::Parser->new; | |||||||||||||||||||
| 129 | 1 | $parser->YYData->{lexer} = $lexer; | |||||||||||||||||||
| 130 | 1 | $parser->YYData->{DATA} = {}; | |||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | $parser->YYParse | ||||||||||||||||||||
| 133 | ( | ||||||||||||||||||||
| 134 | yylex => sub | ||||||||||||||||||||
| 135 | 26 | ▶ | { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 136 | 26 | $_[0]->YYData->{ERR} = 0; | |||||||||||||||||||
| 137 | 26 | my $lexer = $_[0]->YYData->{lexer}; | |||||||||||||||||||
| 138 | 26 | ▶ | return ('', undef) if $lexer->eoi; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 25 | my $token = $lexer->next; | |||||||||||||||||||
| 140 | 25 | return $token->name, $token->text; | |||||||||||||||||||
| 141 | }, | ||||||||||||||||||||
| 142 | yyerror => sub | ||||||||||||||||||||
| 143 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 144 | 0 | $_[0]->YYData->{ERR} = 1; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | 1 | ); | |||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 1 | ▶ | return $parser->YYData->{ERR} == 1 ? undef : $parser->YYData->{DATA}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =back | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =head1 AUTHOR | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | mike eldridge <diz@cpan.org> | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =head1 LICENSE | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 160 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||||||||||||
| 161 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 1 | 1; | |||||||||||||||||||