CGI::Test::Form(3pm)
π CGI::Test::Form β the manual page, bestie edition π
Okay so this is CGI::Test::Form, from the User Contributed Perl Documentation, section 3pm. It's the girlypop who lets you talk to the widgets inside a CGI form. Let's get into it. π
β¨ NAME β¨
CGI::Test::Form β Querying interface to CGI form widgets π
πΈ SYNOPSIS πΈ
my $form = $page->forms->[0]; # first form in CGI::Test::Page
#
# Querying interface, to access form widgets
#
my @buttons = $form->button_list; # ->buttons would give list ref
my $radio_listref = $form->radios; # ->radios_list would give list
my $passwd_widget = $form->input_by_name("password");
my ($login, $passwd) = $form->input_by_name(qw(login password));
my @menus = $form->widgets_matching(sub { $_[0]->is_menu });
my @menus = $form->menu_list; # same as line above
my $rg = $form->radio_groups; # a CGI::Test::Form::Group or undef
#
# <FORM> attributes, as defined by HTML 4.0
#
my $encoding = $form->enctype;
my $action = $form->action;
my $method = $form->method;
my $name = $form->name;
my $accept = $form->accept;
my $accept_charset = $form->accept_charset;
#
# Miscellaneous
#
# Low-level, direct calls normally not needed
$form->reset;
my $new_page = $form->submit;
# Very low-level access
my $html_tree = $form->tree; # HTML::Element form tree
my $page = $form->page; # Page containing this form
#
# Garbage collection -- needed to break circular references
#
$form->delete;
π¦ DESCRIPTION π¦
The CGI::Test::Form class is your little bestie for poking at the content of CGI forms. You don't even have to build these yourself β instances get created automatically by CGI::Test when it's out here analyzing the HTML output of a GET/POST request and bumps into one of these creatures. automatic. iconic. no notes. β¨
This class is genuinely the basis of everything CGI::Test can do, like the way it's the whole backbone of the testing abilities: it hands you the routines you need to query the CGI widgets hanging out in the form β buttons, input areas, menus, etc... π You can query them by type, you can query them by name. And there's also a whole interface specifically for getting at the grouped widgets, like checkboxes and radio buttons. π
Every single widget the queries hand back is a polymorphic object, an heir of CGI::Test::Form::Widget. Here's the vibe check: if the querying interface is basically the human eye β letting you spot a particular graphical item on the browser screen β then the widget interface is basically the mouse and keyboard, letting you actually interact with the graphical components you just located. π±οΈβ¨ If you want the deets on interacting, go see CGI::Test::Form::Widget. Periodt.
Okay and beyond the widget-querying situation, this class also offers a few services to other CGI::Test components β like handling reset and submit actions β which, ngl, you don't really need to use directly in practice. π
Finally, it gives you inspection of the <FORM> tag attributes (encoding type, action, etc...) and β if you really need it β access to the HTML tree of all the <FORM> content. π This interface is based on the HTML::Element class, which represents a tree node. And heads up: the tree is shared with other CGI::Test components, it is not a private copy, bestie. If you're not already familiar with it, go read HTML::Element.
π«Ά MEMORY WARNING, this one's important: if memory is a problem, you must be aware that circular references are used almost everywhere withinCGI::Test. Because Perl's garbage collector literally cannot reclaim objects that are part of such a reference loop, you have to explicitely call thedeletemethod onCGI::Test::Form. Simply forgetting about the reference to that object is not enough. Don't bother with any of this if your regression test scripts die quickly. π
more coming in part 2, don't you dare close the tab πΈ
π INTERFACE π
okay so bestie, this is mostly a querying interface. most routines hand you widget objects back, via lists or list refs. for the tea on what widget objects actually do and how they're classified, go read CGI::Test::Form::Widget. β¨
The order of the widgets in the returned lists? literally the same order they show up in the HTML. no cap. π
β¨ Type Querying Interface β¨
two groups of routines here: one gives you expanded lists, the other gives list references. table time but make it cute.
The Item Polymorphic Type column is about the polymorphic dynamic type of the items inside the list: every item is guaranteed to be at least that type, but it can be a descendant. types are abridged, so you gotta prepend CGI::Test::Form:: to get the real type. π
button_list(expanded) /buttons(list ref) β items are at leastWidget::Buttoncheckbox_list/checkboxesβ items are at leastWidget::Box::Checkhidden_list/hiddenβ items are at leastWidget::Hiddeninput_list/inputsβ items are at leastWidget::Inputmenu_list/menusβ items are at leastWidget::Menuradio_list/radiosβ items are at leastWidget::Box::Radiosubmit_list/submitsβ items are at leastWidget::Button::Submitwidget_list/widgetsβ items are at leastWidget
For instance:
my @widgets = @{$form->widgets}; # heavy style
my @widgets = $form->widget_list; # light style
A given widget can show up in several lists, i.e. the above do not form a partition over the widget set. for example, a submit button would appear in widget_list (which lists all widgets), in button_list, and in submit_list. π₯
πΈ Name Querying Interface πΈ
These routines take a name or a list of names, and return the widgets whose parameter name is exactly the given name (string comparison). You can query all widgets, or a particular class β like all buttons, or all input fields.
Two groups of routines:
- One group allows for multiple name queries, and returns a list of widgets, one entry for each listed name. Some widgets like radio buttons may have multiple instances bearing the same name, and in that case only one is returned. When querying for one name, you're allowed to use scalar context:
my @hidden = $form->hidden_by_name("foo", "bar");
my ($context) = $form->hidden_by_name("context");
my $context = $form->hidden_by_name("context");
When no widget (of that particular type) bearing the requested name is found, undef is returned for that particular slot, so don't blindly make method calls on each returned value. π
We shall call that group of query routines the by-name group.
- The other group allows for a single name query, but returns a list of all the widgets (of some particular type when not querying the whole widget list) bearing that name.
my @hidden = $form->hidden_named("foo");
Don't assume that only radios and checkboxes can have multiple instances bearing the same name. π
We shall call that group of query routines the all-named group.
The available routines are listed below. Note that by-name queries are singular, because there is at most one returned widget per name asked, whereas all-named queries are plural, where possible.
The Item Polymorphic Type column refers to the polymorphic dynamic type of items held within the list: each defined item is guaranteed to at least be of that type, but can be a descendant. Types are listed in the abridged form, and you have to prepend the string CGI::Test::Form:: in front of them to get the real type. π
button_by_name(by-name) /buttons_named(all-named) β items are at leastWidget::Buttoncheckbox_by_name/checkboxes_namedβ items are at leastWidget::Box::Checkhidden_by_name/hidden_namedβ items are at leastWidget::Hiddeninput_by_name/inputs_namedβ items are at leastWidget::Inputmenu_by_name/menus_namedβ items are at leastWidget::Menuradio_by_name/radios_namedβ items are at leastWidget::Box::Radiosubmit_by_name/submits_namedβ items are at leastWidget::Button::Submitwidget_by_name/widgets_namedβ items are at leastWidget
π¦ Match Querying Interface π¦
This is a general interface, which invokes a matching callback on each widget of a particular category. The signature of the matching routines is:
my @matching = $form->widgets_matching(sub {code}, $arg);
and the callback is invoked as:
callback($widget, $arg);
A widget is kept if, and only if, the callback returns true. Be sure to write your callback so that it only uses calls that apply to the particular widget. When you know you're matching on menu widgets, you can call menu-specific features, but should you use that same callback for buttons, you would get a runtime error. π
Each matching routine returns a list of matching widgets. Using the $arg parameter is optional, and should be avoided unless you have no other choice, so as to be as stateless as possible.
The following routines are available, along with the polymorphic widget type to be expected in the callback. As usual, you must prepend the string CGI::Test::Form:: to get the real type. β¨
buttons_matchingβ items are at leastWidget::Buttoncheckboxes_matchingβ items are at leastWidget::Box::Checkhidden_matchingβ items are at leastWidget::Hiddeninputs_matchingβ items are at leastWidget::Inputmenus_matchingβ items are at leastWidget::Menuradios_matchingβ items are at leastWidget::Box::Radiosubmits_matchingβ items are at leastWidget::Button::Submitwidgets_matchingβ items are at leastWidget
For instance:
my @menus = $form->widgets_matching(sub { $_[0]->is_menu });
my @color = $form->widgets_matching(
sub { $_[0]->is_menu && $_[0]->name eq "color" }
);
is an inefficient way of saying:
my @menus = $form->menu_list;
my @color = $form->menus_matching(sub { $_[0]->name eq "color" });
and the latter can further be rewritten as:
my @color = $form->menus_named("color");
π Form Interface π
This gives you an interface to get at the attributes of the <FORM> tag. For instance:
my $enctype = $form->enctype;
to get at the encoding type of that particular form. The following attributes are available:
accept
accept_charset
action
enctype
method
name
as defined by HTML 4.0. π
π Group Querying Interface π
There are two kinds of widgets that are architecturally groupped (yes, that's the word, meaning more than one instance of that widget can bear the same name): radio buttons and checkboxes (although you may have a single standalone checkbox).
All radio buttons and checkboxes defined in a form are automatically inserted into a group of their own, which is an instance of the CGI::Test::Form::Group class. This class contains all the defined groups for a particular kind. The routines:
checkbox_groups
radio_groups
give you access to the CGI::Test::Form::Group container. Both routines may return undef when there is no checkbox or radio button in the form. See CGI::Test::Form::Group for its querying interface. π
π§ Memory Cleanup π§
You must call the delete method to break the circular references if you wish to dispose of the object. periodt. π
β Internal Interface β
The following routines are available internally:
resetβ Reset the form state, restoring all the controls to the value they had upon entry.submitβ Submit the form, returning aCGI::Test::Pagereply.
π AUTHORS π
The original author is Raphael Manfredi.
Steven Hilton was long time maintainer of this module.
Current maintainer is Alexander Tokarev <tokarev@cpan.org>.
π SEE ALSO π
CGI::Test(3)CGI::Test::Form::Widget(3)CGI::Test::Form::Group(3)CGI::Test::Page(3)HTML::Element(3)
okay bestie, that's the whole vibe. this page came from perl v5.34.0, dated 2022-06-10, and it's literally CGI::Test::Form(3pm). go slay some forms π
β¨
π¦ where this page comes from
- debian bookworm: libcgi-test-perl 1.111-2 Β· Artistic-1.0-Perl
- debian trixie: libcgi-test-perl 1.111-2 Β· Artistic-1.0-Perl
- ubuntu noble: libcgi-test-perl 1.111-2 Β· Artistic-1.0-Perl
- ubuntu resolute: libcgi-test-perl 1.111-2 Β· Artistic-1.0-Perl
- ubuntu stonking: libcgi-test-perl 1.111-2 Β· Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms