Form(3)
π 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 anHTML::Elementobject containing aFORMelement or anISINDEXelement 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 basicallynewbut it can apply to any part of anHTML::Elementtree, root included β it builds a new form processor for everyFORMelement 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$baseparameter that you gave to thenewconstructor.link()β gives you theactionattribute 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 themethodattribute 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 anISINDEXelement. in that case the form will have only one field: aninput/textfield namedkeywords.name()β gives you thenameattribute 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 thehiddenandsubmitelements, because those are already filled with a value (aka declared "closed"), or β in the case ofsubmitβ 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, sois_selectionreturns 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 withselectedin 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 thebuttonsmethod.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 anHTTP::Requestobject (viaHTTP::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
optgrouptags is missing (as it is with allmost all current browsers, so honestly not a great loss). - the
buttontag (HTML 4.0) is just handled as an alias for theinputtag β 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 π
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
- fedora 43: perl-HTTP-Request-Form 0.952-33.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms