Form(3)

πŸ“¦
version 0.952 Β· 2 versions
, in fedora 43

πŸ’– Form(3) β€” HTTP::Request::Form πŸ’–

okay bestie, gather round, because this one is from the User Contributed Perl Documentation collection and it is so useful if you've ever wanted your Perl code to just... fill out a web form for you. no cap.


✨ NAME ✨

HTTP::Request::Form β€” construct HTTP::Request objects for form processing. it's giving automated form-filling main character energy 🫢


🌸 SYNOPSIS 🌸

okay so imagine you wanna query Altavista for "perl" straight from the commandline. this is the vibe:

use URI::URL;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use HTTP::Request::Form;
use HTML::TreeBuilder 3.0;

my $ua = LWP::UserAgent->new;
my $url = url 'http://www.altavista.digital.com/';
my $res = $ua->request(GET $url);
my $tree = HTML::TreeBuilder->new;
$tree->parse($res->content);
$tree->eof();

my @forms = $tree->find_by_tag_name('FORM');
die "What, no forms in $url?" unless @forms;
my $f = HTTP::Request::Form->new($forms[0], $url);
$f->field("q", "perl");
my $response = $ua->request($f->press("search"));
print $response->content if $response->is_success;

literally the whole pipeline: fetch the page, parse it, find the form, fill it in, press the button, print the tea. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so this module is an extension of the HTTP::Request suite. what it does is make processing forms in a user agent easy β€” you can fill out fields, query fields, selections and buttons, and press buttons. that's the whole vibe.

it works off parse trees of documents generated by HTML::TreeBuilder (especially the forms parts you get extracted with extract_links), and then it builds its own internal representation of forms, and from that it generates the request objects used to process the form application. iconic honestly. ✨


πŸ‘‘ CLASS METHODS πŸ‘‘

  • new($form [, $base [, $debug]]) β€” the new-method constructs a brand new form processor, bestie. it takes an HTML::Element object containing a FORM element or an ISINDEX element as its single parameter. if a base-url gets passed in as an extra parameter, that's what's used to make the form-url absolute relative to the given URL. and if debugging is true, the functions below get a little bit "talky" on stdio. πŸ™ˆ
  • new_many($tree_part [, $base [, $debug]]) β€” this one returns a list of freshly constructed form processors. it's basically new but it can apply to any part of an HTML::Element tree, root included β€” it builds a new form processor for every FORM element at or under $tree_part.

heads up: that return list might have zero, one or many new objects in it, depending on how many FORM (or ISINDEX) elements turned up. no promises.

also β€” form elements (like INPUT, etc.) that show up outside of FORM elements get counted as part of the preceding FORM element. (and if there's no preceding FORM element, they just get ignored, sorry babe.) this is genuinely useful with the weird parse trees you get from basd HTML in or around FORM elements. if you need to override that behaviour, call this instead:

map HTTP::Request::Form->new($_), $tree->find_by_tag_name('FORM');

πŸ’œ INSTANCE METHODS πŸ’œ

  • base() β€” hands you back the $base parameter that you gave to the new constructor.
  • link() β€” gives you the action attribute of the original form structure. this value is cached inside the form processor, so once you've made the form processor you can safely delete the form structure. go off. πŸŽ€
  • method() β€” gives you the method attribute of the original form structure. also cached in the form processor, so you can safely delete the form structure as soon as you've created the form processor.
  • isindex() β€” returns true if this came from an original form structure that was actually an ISINDEX element. in that case the form will have only one field: an input/text field named keywords.
  • name() β€” gives you the name attribute of the original form structure. yep, cached too, so you can safely delete the form structure after you've created the form processor.
  • fields() β€” delivers a list of fieldnames that are of the "open" type. this excludes the hidden and submit elements, because those are already filled with a value (aka declared "closed"), or β€” in the case of submit β€” they're buttons, and only one of those must be used.
  • allfields() β€” delivers a list of all the fieldnames, in the order they occured in the form-source, excluding the submit fields.
  • field($name [, $value]) β€” retrieves or sets a field-value. the field is identified by its name. you have to be sure you only put an allowed value into the field, okay? be responsible. πŸ™
  • field_type($name) β€” gives you the type of the named field so you can distinguish on that type. (this is literally the only way to tell selections and radio buttons apart.)
  • is_selection($name) β€” tests whether a field is a selection or an input. radio-buttons get used the same way as standard selection fields, so is_selection returns a true value for radio buttons too! (of course, only one value is submitted for a radio button. one. single. value.) πŸ’€
  • field_selection($name) β€” delivers the array of the options of a selection. the element marked with selected in the source is given as the default value. this works exactly the same way for radio buttons, because they're just handled as a special case of selections!
  • is_checkbox($name) β€” tells you if a field is a checkbox. if it is, there are several support methods to make use of checkboxes' special features β€” for example, the fact that it's only sent if it's checked. slay.
  • checkboxes() β€” delivers a list of all checkbox fields, much in the same way as the buttons method.
  • checkbox_check($name)
  • checkbox_uncheck($name)
  • checkbox_toggle($name) β€” these set, unset or toggle the checkbox checked state. checkbox values are only added to the result if they're checked. periodt.
  • checkbox_ischecked($name) β€” tells you whether a checkbox is checked or not. this matters if you want to analyze the state of fields directly after the parse.
  • buttons() β€” delivers a list of all defined and named buttons of a form.
  • button($button [, $value]) β€” gets or sets the value of a button. normally you only need the getting part. the value of a button is a reference to an array of values (because a button can exist multiple times, and we respect that).
  • button_type($button) β€” gives you the type of a button (submit/reset/image). the result is an array of type names, since a button with one name can exist multiple times.
  • button_exists($button) β€” true if the named button exists, false (undef) otherwise.
  • referer([$value]) β€” returns or sets the referer header for a request. super useful if a CGI needs a set referer for authentication. 🫢
  • press([$name [, $coord ] [, $number]]) β€” this method creates an HTTP::Request object (via HTTP::Request::Common) that sends the formdata to the server using the requested method. if you give it a button-name, that button is the one used. if you give no button name, it assumes a button without a name and just leaves out this last parameter. if the number of the button is given, that button value is delivered. if the number isn't given, 0 (the first button of this name) is assumed.

the coord parameter comes in handy when you've got an image button. if that's the case, the button press will simulate a press at coordinates [2,2] unless you provide an anonymous array with different coordinates. very demure, very mindful. πŸ’…

  • dump() β€” dumps the form-data on stdio for debugging purposes. πŸ”

🌈 SEE ALSO 🌈

HTTP::Request, HTTP::Request::Common, LWP::UserAgent, HTML::Element, URI::URL


πŸŽ€ INSTALLATION πŸŽ€

perl Makefile.PL
make install

or see perlmodinstall. either way, you got this. ✨


⭐ REQUIRES ⭐

Perl version 5.004 or later

HTTP::Request::Common
HTML::TreeBuilder
LWP::UserAgent

πŸ“ VERSION πŸ“

HTTP::Request::Form version 0.9, February 8th, 2001. yes she's vintage. vintage is iconic.


🧚 RESTRICTIONS 🧚

ngl, only a subset of all possible form elements are currently supported. the list of supported tags as of this version includes:

INPUT/CHECKBOX
INPUT/HIDDEN
INPUT/IMAGE
INPUT/RADIO
INPUT/RESET
INPUT/SUBMIT
INPUT/FILE
INPUT/* (are all handled as simple text entry)
OPTION
SELECT
TEXTAREA
ISINDEX

πŸ’€ BUGS πŸ’€

okay bestie, deep breaths, here's the list:

  • there is currently no support for multiple selections. you can do them yourself by setting a selection to a comma-delimited list of values, though. DIY queen. πŸ’…
  • multiple fields are not properly handled β€” only the last value is available. the exception is buttons, those are handled in the right way.
  • if there are several fields with the same name, you can only set the value of the first of these fields (this is especially problematic with checkboxes). it does work with buttons that have the same name, though β€” you can press each instance identified by number.
  • error-checking is currently very limited (not to say nonexistant). 😭
  • support for HTML 4.0 optgroup tags is missing (as it is with allmost all current browsers, so honestly not a great loss).
  • the button tag (HTML 4.0) is just handled as an alias for the input tag β€” this is of course incorrect, but it's sufficient for supporting the usual button types.
none of this is undefined behaviour or anything, it's just... limited. set your expectations accordingly, babe. 🫢

Copyright 1998, 1999, 2000 Georg Bauer <Georg_Bauer@muensterland.org>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


🌟 MAJOR CONTRIBUTORS 🌟

Sean M. Burke (ISINDEX, new_many) β€” legend behaviour, no notes. πŸ‘‘


and that's the whole page, bestie! this one's from perl v5.42.0, dated 2025-07-25, and it lives in the Form(3) section of the manual. go fill out some forms. not like that. πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from