| 1 | package CGI::FormBuilder::Source::YAML; | ||||||||||||||||||||||||||||
| 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 | ▶ | use YAML::Syck; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use CGI::FormBuilder::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '1.0008'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 14 | 1 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 15 | 1 | my %opt = @_; | |||||||||||||||||||||||||||
| 16 | 1 | return bless \%opt, $class; | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=9 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 21 | 1 | ▶ | my $file = shift || $self->{source}; | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $self->{'source'}
MC/DC0%0/2
shift() || $self->{'source'}
shift()
$self->{'source'}
Truth table33%1/3
A || B
A= shift()B= $self->{'source'}
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | local $YAML::Syck::LoadCode = 1; | |||||||||||||||||||||||||||
| 24 | 1 | local $YAML::Syck::UseCode = 1; | |||||||||||||||||||||||||||
| 25 | 1 | local $YAML::Syck::DumpCode = 1; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | $CGI::FormBuilder::Util::DEBUG ||= $self->{debug} if ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$CGI::FormBuilder::Util::DEBUG ||= $self->{"debug"}
MC/DC0%0/2
$CGI::FormBuilder::Util::DEBUG ||= $self->{"debug"}
$CGI::FormBuilder::Util::DEBUG
$self->{"debug"}
Truth table33%1/3
A ||= B
A= $CGI::FormBuilder::Util::DEBUGB= $self->{"debug"}
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | puke("file must be only one scalar file name") if ref $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | my $formopt = LoadFile($file); | |||||||||||||||||||||||||||
| 32 | 1 | ▶ | puke("loaded file '$file' is not hashref") if ref $formopt ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | debug 1, "processing YAML::Syck file '$file' as input source"; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # add in top-level options: | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | map { $formopt->{$_} = $self->{$_} if !exists $formopt->{$_} } keys %{$self}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # whork in the function refs: | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | $self->_assign_references($formopt, 1) if ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | my %lame = ( %{$formopt} ); | |||||||||||||||||||||||||||
| 43 | 1 | debug 1, "YAML form definition is:", Dump(\%lame); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | return wantarray ? %{$formopt} : $formopt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 8 | ▶ | sub _assign_references { | ||||||||||||||||||||||||||
|
Subroutine
_assign_references: called CC=11 SCAR=29.6
| |||||||||||||||||||||||||||||
| 49 | 8 | my ($self, $hashref, $stacklevel) = @_; | |||||||||||||||||||||||||||
| 50 | 8 | $stacklevel++; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | NODE: | ||||||||||||||||||||||||||||
| 53 | 8 | foreach my $node (values %{$hashref}) { | |||||||||||||||||||||||||||
| 54 | 31 | my $ref = ref $node; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 31 | ▶ | if ($ref eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 57 | 7 | $self->_assign_references($node, $stacklevel); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | elsif (!$ref) { | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 20 | debug 1, "node is '$node'\n"; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 20 | ▶ | if ( $node =~ m{ \A \\ ([&\$%@]) (.*) \z }xms ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | my ($reftype, $refstr) = ($1, $2); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | if ($refstr =~ m{ :: }xms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | # already know where it is. assign it. | ||||||||||||||||||||||||||||
| 69 | 0 | my $subref = undef; | |||||||||||||||||||||||||||
| 70 | 0 | debug 1, "assigning direct pkg ref for '$reftype$refstr'"; | |||||||||||||||||||||||||||
| 71 | 0 | eval "\$subref = \\$reftype$refstr"; | |||||||||||||||||||||||||||
| 72 | 0 | my $err = $@; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | debug 1, "eval error '$err'" if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | debug 1, "subref is '$subref'"; | |||||||||||||||||||||||||||
| 75 | 0 | $node = $subref; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | my $l = $stacklevel; | |||||||||||||||||||||||||||
| 80 | 1 | my $subref = undef; | |||||||||||||||||||||||||||
| 81 | LEVELUP: | ||||||||||||||||||||||||||||
| 82 | 1 | ▶ | while (my $pkg = caller($l++)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 1 | debug 1, "looking up at lev $l for ref '$refstr' in '$pkg'"; | |||||||||||||||||||||||||||
| 84 | 1 | my $evalstr = "\$subref = \\$reftype$pkg\::$refstr"; | |||||||||||||||||||||||||||
| 85 | 1 | debug 1, "eval '$evalstr'"; | |||||||||||||||||||||||||||
| 86 | 1 | eval $evalstr; | |||||||||||||||||||||||||||
| 87 | 1 | ▶ | if (!$@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | $node = $subref; | |||||||||||||||||||||||||||
| 89 | 1 | last LEVELUP; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 1 | debug 1, "assgnd ref '$node' for '$reftype$refstr'"; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | elsif ( $node =~ m/ \A eval \s* { (.*) } \s* \z /xms ) { | ||||||||||||||||||||||||||||
| 96 | 0 | my $evalstr = $1; | |||||||||||||||||||||||||||
| 97 | 0 | debug 1, "eval '$evalstr'"; | |||||||||||||||||||||||||||
| 98 | 0 | my $result = eval $evalstr; | |||||||||||||||||||||||||||
| 99 | 0 | my $err = $@; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($err) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | debug 1, "eval error '$err'"; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | 0 | debug 1, "assgnd ref '$node' for eval"; | |||||||||||||||||||||||||||
| 105 | 0 | $node = $result; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 8 | return; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | 1; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 NAME | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | CGI::FormBuilder::Source::YAML - Initialize FormBuilder from YAML file | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | use CGI::FormBuilder; | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | my $form = CGI::FormBuilder->new( | ||||||||||||||||||||||||||||
| 125 | source => { | ||||||||||||||||||||||||||||
| 126 | source => 'form.fb', | ||||||||||||||||||||||||||||
| 127 | type => 'YAML', | ||||||||||||||||||||||||||||
| 128 | }, | ||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | my $lname = $form->field('lname'); # like normal | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | This reads a YAML (YAML::Syck) file that contains B<FormBuilder> | ||||||||||||||||||||||||||||
| 136 | config options and returns a hash to be fed to CGI::FormBuilder->new(). | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Instead of the syntax read by CGI::FormBuilder::Source::File, | ||||||||||||||||||||||||||||
| 139 | it uses YAML syntax as read by YAML::Syck. That means you | ||||||||||||||||||||||||||||
| 140 | fully specify the entire data structure. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | LoadCode is enabled, so you can use YAML syntax for defining subroutines. | ||||||||||||||||||||||||||||
| 143 | This is convenient if you have a function that generates validation | ||||||||||||||||||||||||||||
| 144 | subrefs, for example, I have one that can check profanity using Regexp::Common. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | validate: | ||||||||||||||||||||||||||||
| 147 | myfield: | ||||||||||||||||||||||||||||
| 148 | javascript: /^[\s\S]{2,50}$/ | ||||||||||||||||||||||||||||
| 149 | perl: !!perl/code: >- | ||||||||||||||||||||||||||||
| 150 | { My::Funk::fb_perl_validate({ | ||||||||||||||||||||||||||||
| 151 | min => 2, | ||||||||||||||||||||||||||||
| 152 | max => 50, | ||||||||||||||||||||||||||||
| 153 | profanity => 'check' | ||||||||||||||||||||||||||||
| 154 | })->(shift); | ||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 POST PROCESSING | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | There are two exceptions to "pure YAML syntax" where this module | ||||||||||||||||||||||||||||
| 160 | does some post-processing of the result. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 REFERENCES (ala CGI::FormBuilder::Source::File) | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | You can specify references as string values that start with | ||||||||||||||||||||||||||||
| 165 | \&, \$, \@, or \% in the | ||||||||||||||||||||||||||||
| 166 | same way you can with CGI::FormBuilder::Source::File. If you have | ||||||||||||||||||||||||||||
| 167 | a full direct package reference, it will look there, otherwise | ||||||||||||||||||||||||||||
| 168 | it will traverse up the caller stack and take the first it finds. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | For example, say your code serves multiple sites, and a menu | ||||||||||||||||||||||||||||
| 171 | gets different options depending on the server name requested: | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # in My::Funk: | ||||||||||||||||||||||||||||
| 174 | our $food_options = { | ||||||||||||||||||||||||||||
| 175 | www.meats.com => [qw( beef chicken horta fish )], | ||||||||||||||||||||||||||||
| 176 | www.veggies.com => [qw( carrot apple quorn radish )], | ||||||||||||||||||||||||||||
| 177 | }; | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # in source file: | ||||||||||||||||||||||||||||
| 180 | options: \@{ $My::Funk::food_options->{ $ENV{SERVER_NAME} } } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 EVAL STRINGS | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | You can specify an eval statement. You could achieve the same | ||||||||||||||||||||||||||||
| 185 | example a different way: | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | options: eval { $My::Funk::food_options->{ $ENV{SERVER_NAME} }; } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | The cost either way is about the same -- the string is eval'd. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 EXAMPLE | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | method: GET | ||||||||||||||||||||||||||||
| 194 | header: 0 | ||||||||||||||||||||||||||||
| 195 | title: test | ||||||||||||||||||||||||||||
| 196 | name: test | ||||||||||||||||||||||||||||
| 197 | action: /test | ||||||||||||||||||||||||||||
| 198 | submit: test it | ||||||||||||||||||||||||||||
| 199 | linebreaks: 1 | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | required: | ||||||||||||||||||||||||||||
| 202 | - test1 | ||||||||||||||||||||||||||||
| 203 | - test2 | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | fields: | ||||||||||||||||||||||||||||
| 206 | - test1 | ||||||||||||||||||||||||||||
| 207 | - test2 | ||||||||||||||||||||||||||||
| 208 | - test3 | ||||||||||||||||||||||||||||
| 209 | - test4 | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | fieldopts: | ||||||||||||||||||||||||||||
| 212 | test1: | ||||||||||||||||||||||||||||
| 213 | type: text | ||||||||||||||||||||||||||||
| 214 | size: 10 | ||||||||||||||||||||||||||||
| 215 | maxlength: 32 | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | test2: | ||||||||||||||||||||||||||||
| 218 | type: text | ||||||||||||||||||||||||||||
| 219 | size: 10 | ||||||||||||||||||||||||||||
| 220 | maxlength: 32 | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | test3: | ||||||||||||||||||||||||||||
| 223 | type: radio | ||||||||||||||||||||||||||||
| 224 | options: | ||||||||||||||||||||||||||||
| 225 | - | ||||||||||||||||||||||||||||
| 226 | - 1 | ||||||||||||||||||||||||||||
| 227 | - Yes | ||||||||||||||||||||||||||||
| 228 | - | ||||||||||||||||||||||||||||
| 229 | - 0 | ||||||||||||||||||||||||||||
| 230 | - No | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | test4: | ||||||||||||||||||||||||||||
| 233 | options: \@test4opts | ||||||||||||||||||||||||||||
| 234 | sort: \&Someother::Package::sortopts | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | validate: | ||||||||||||||||||||||||||||
| 237 | test1: /^\w{3,10}$/ | ||||||||||||||||||||||||||||
| 238 | test2: | ||||||||||||||||||||||||||||
| 239 | javascript: EMAIL | ||||||||||||||||||||||||||||
| 240 | perl: eq 'test@test.foo' | ||||||||||||||||||||||||||||
| 241 | test3: | ||||||||||||||||||||||||||||
| 242 | - 0 | ||||||||||||||||||||||||||||
| 243 | - 1 | ||||||||||||||||||||||||||||
| 244 | test4: \@test4opts | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | You get the idea. A bit more whitespace, but it works in a | ||||||||||||||||||||||||||||
| 247 | standardized way. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head1 METHODS | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =head2 new() | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | Normally not used directly; it is called from CGI::FormBuilder. | ||||||||||||||||||||||||||||
| 254 | Creates the C<CGI::FormBuilder::Source::YAML> object. Arguments | ||||||||||||||||||||||||||||
| 255 | from the 'source' hash passed to CGI::FormBuilder->new() will | ||||||||||||||||||||||||||||
| 256 | become defaults, unless specified in the file. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =head2 parse($source) | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | Normally not used directly; it is called from CGI::FormBuilder. | ||||||||||||||||||||||||||||
| 261 | Parses the specified source file. No fancy params -- | ||||||||||||||||||||||||||||
| 262 | just a single filename is accepted. If the file isn't | ||||||||||||||||||||||||||||
| 263 | acceptable to YAML::Syck, I suppose it will die. | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | L<CGI::FormBuilder>, L<CGI::FormBuilder::Source> | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | Copyright (c) 2006 Mark Hedges <hedges@ucsd.edu>. All rights reserved. | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | This module is free software; you may copy it under terms of | ||||||||||||||||||||||||||||
| 276 | the Perl license (GNU General Public License or Artistic License.) | ||||||||||||||||||||||||||||
| 277 | http://www.opensource.org/licenses/index.html | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =cut | ||||||||||||||||||||||||||||