CGI::F...::YAML(3pm)
CGI::F...::YAML(3pm) β bestie edition π
okay so you're reading the manual page for CGI::FormBuilder::Source::YAML, part of the User Contributed Perl Documentation β¨. It's a Perl module, it's iconic, and it's about to make your forms read YAML. sit down, grab a boba π§, let's go.
πΈ NAME πΈ
CGI::FormBuilder::Source::YAML β initialize FormBuilder from a YAML file. no cap, that's the whole vibe π¦
β¨ SYNOPSIS β¨
like, the standard entrance:
use CGI::FormBuilder;
my $form = CGI::FormBuilder->new(
source => {
source => 'form.fb',
type => 'YAML',
},
);
my $lname = $form->field('lname'); # like normal
and then $form->field('lname') just... works. like normal. π
π DESCRIPTION π
So this module reads a YAML file (specifically via YAML::Syck, iykyk) that's holding all your FormBuilder config options, and it hands you back a hash that gets fed straight into CGI::FormBuilder->new(). that's the whole job. main character energy fr.
Now β instead of the syntax that CGI::FormBuilder::Source::File reads, this one uses YAML syntax as read by YAML::Syck. Which means, bestie, you have to fully specify the entire data structure. no shortcuts, no halfsies, the whole thing. periodt.
LoadCode is enabled, which is lowkey the best part, because it means you can use YAML syntax to define subroutines π. This is so convenient if you've got a function that generates validation subrefs β for example, the author has one that checks profanity using Regexp::Common. iconic.
validate:
myfield:
javascript: /^[\s\S]{2,50}$/
perl: !!perl/code: >-
{ My::Funk::fb_perl_validate({
min => 2,
max => 50,
profanity => 'check'
})->(shift);
}
π¦ POST PROCESSING π¦
There are two exceptions to "pure YAML syntax" where this module does some post-processing on the result. just two. not me keeping count. π
π REFERENCES (ala CGI::FormBuilder::Source::File) π
You can specify references as string values that start with \&, \$, \@, or \% β same way you can with CGI::FormBuilder::Source::File. And here's the tea: if you've got a full direct package reference, it'll look right there. Otherwise it traverses up the caller stack and takes the first one it finds. slay.
For example β say your code serves multiple sites, and a menu gets different options depending on the server name requested:
# in My::Funk:
our $food_options = {
www.meats.com => [qw( beef chicken horta fish )],
www.veggies.com => [qw( carrot apple quorn radish )],
};
# in source file:
options: \@{ $My::Funk::food_options->{ $ENV{SERVER_NAME} } }
look at that. chef's kiss. π
β EVAL STRINGS β
You can also specify an eval statement. Same example, different route:
options: eval { $My::Funk::food_options->{ $ENV{SERVER_NAME} }; }
The cost either way is about the same β the string is eval'd. π
π EXAMPLE π
here's a whole YAML file, verbatim, because it's that girl:
method: GET
header: 0
title: test
name: test
action: /test
submit: test it
linebreaks: 1
required:
- test1
- test2
fields:
- test1
- test2
- test3
- test4
fieldopts:
test1:
type: text
size: 10
maxlength: 32
test2:
type: text
size: 10
maxlength: 32
test3:
type: radio
options:
-
- 1
- Yes
-
- 0
- No
test4:
options: \@test4opts
sort: \&Someother::Package::sortopts
validate:
test1: /^\w{3,10}$/
test2:
javascript: EMAIL
perl: eq 'test@test.foo'
test3:
- 0
- 1
test4: \@test4opts
You get the idea π‘. A bit more whitespace, but it works in a standardized way. we love consistency in this house.
π METHODS π
π«Ά new()
Normally not used directly β it gets called from CGI::FormBuilder. It creates the CGI::FormBuilder::Source::YAML object β¨. Arguments from the 'source' hash passed to CGI::FormBuilder->new() will become defaults, unless they're specified in the file. file wins. always.
π«Ά parse($source)
Also normally not used directly β it's called from CGI::FormBuilder too. It parses the specified source file. No fancy params, bestie β just a single filename is accepted. And if the file isn't acceptable to YAML::Syck... I suppose it will die. ππ that's it, that's the error handling.
π₯ SEE ALSO π₯
CGI::FormBuilderCGI::FormBuilder::Source
go read those too, they're the supporting cast and they ate. π
π AUTHOR π
Copyright (c) 2006 Mark Hedges <hedges@ucsd.edu>. All rights reserved. π
π LICENSE π
This module is free software; you may copy it under terms of the Perl license (GNU General Public License or Artistic License.) http://www.opensource.org/licenses/index.html
and that's the whole page, bestie π¦ β this one came from perl v5.34.0, dated 2022-06-09, and honestly? she's a classic. go build a form. slay π β¨πΈ
π¦ where this page comes from
- debian bookworm: libcgi-formbuilder-source-yaml-perl 1.0.8-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libcgi-formbuilder-source-yaml-perl 1.0.8-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcgi-formbuilder-source-yaml-perl 1.0.8-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms