⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form.pm

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 PM
📖 第 1 页 / 共 3 页
字号:
=item $form->param( $name, $value, ... )=item $form->param( $name, \@values )Alternative interface to examining and setting the values of the form.If called without arguments then it returns the names of all theinputs in the form.  The names will not repeat even if multiple inputshave the same name.  In scalar context the number of different namesis returned.If called with a single argument then it returns the value or valuesof inputs with the given name.  If called in scalar context only thefirst value is returned.  If no input exists with the given name, thenC<undef> is returned.If called with 2 or more arguments then it will set values of thenamed inputs.  This form will croak if no inputs have the given nameor if any of the values provided does not fit.  Values can also beprovided as a reference to an array.  This form will allow unsettingall values with the given name as well.This interface resembles that of the param() function of the CGImodule.=cutsub param {    my $self = shift;    if (@_) {        my $name = shift;        my @inputs;        for ($self->inputs) {            my $n = $_->name;            next if !defined($n) || $n ne $name;            push(@inputs, $_);        }        if (@_) {            # set            die "No '$name' parameter exists" unless @inputs;	    my @v = @_;	    @v = @{$v[0]} if @v == 1 && ref($v[0]);            while (@v) {                my $v = shift @v;                my $err;                for my $i (0 .. @inputs-1) {                    eval {                        $inputs[$i]->value($v);                    };                    unless ($@) {                        undef($err);                        splice(@inputs, $i, 1);                        last;                    }                    $err ||= $@;                }                die $err if $err;            }	    # the rest of the input should be cleared	    for (@inputs) {		$_->value(undef);	    }        }        else {            # get            my @v;            for (@inputs) {		if (defined(my $v = $_->value)) {		    push(@v, $v);		}            }            return wantarray ? @v : $v[0];        }    }    else {        # list parameter names        my @n;        my %seen;        for ($self->inputs) {            my $n = $_->name;            next if !defined($n) || $seen{$n}++;            push(@n, $n);        }        return @n;    }}=item $form->try_others( \&callback )This method will iterate over all permutations of unvisited enumeratedvalues (<select>, <radio>, <checkbox>) and invoke the callback foreach.  The callback is passed the $form as argument.  The return valuefrom the callback is ignored and the try_others() method itself doesnot return anything.=cutsub try_others{    my($self, $cb) = @_;    my @try;    for (@{$self->{'inputs'}}) {	my @not_tried_yet = $_->other_possible_values;	next unless @not_tried_yet;	push(@try, [\@not_tried_yet, $_]);    }    return unless @try;    $self->_try($cb, \@try, 0);}sub _try{    my($self, $cb, $try, $i) = @_;    for (@{$try->[$i][0]}) {	$try->[$i][1]->value($_);	&$cb($self);	$self->_try($cb, $try, $i+1) if $i+1 < @$try;    }}=item $request = $form->make_requestWill return an C<HTTP::Request> object that reflects the current settingof the form.  You might want to use the click() method instead.=cutsub make_request{    my $self = shift;    my $method  = uc $self->{'method'};    my $uri     = $self->{'action'};    my $enctype = $self->{'enctype'};    my @form    = $self->form;    if ($method eq "GET") {	require HTTP::Request;	$uri = URI->new($uri, "http");	$uri->query_form(@form);	return HTTP::Request->new(GET => $uri);    }    elsif ($method eq "POST") {	require HTTP::Request::Common;	return HTTP::Request::Common::POST($uri, \@form,					   Content_Type => $enctype);    }    else {	Carp::croak("Unknown method '$method'");    }}=item $request = $form->click=item $request = $form->click( $name )=item $request = $form->click( $x, $y )=item $request = $form->click( $name, $x, $y )Will "click" on the first clickable input (which will be of typeC<submit> or C<image>).  The result of clicking is an C<HTTP::Request>object that can then be passed to C<LWP::UserAgent> if you want toobtain the server response.If a $name is specified, we will click on the first clickable inputwith the given name, and the method will croak if no clickable inputwith the given name is found.  If $name is I<not> specified, then itis ok if the form contains no clickable inputs.  In this case theclick() method returns the same request as the make_request() methodwould do.If there are multiple clickable inputs with the same name, then thereis no way to get the click() method of the C<HTML::Form> to click onany but the first.  If you need this you would have to locate theinput with find_input() and invoke the click() method on the giveninput yourself.A click coordinate pair can also be provided, but this only makes adifference if you clicked on an image.  The default coordinate is(1,1).  The upper-left corner of the image is (0,0), but some badlycoded CGI scripts are known to not recognize this.  Therefore (1,1) wasselected as a safer default.=cutsub click{    my $self = shift;    my $name;    $name = shift if (@_ % 2) == 1;  # odd number of arguments    # try to find first submit button to activate    for (@{$self->{'inputs'}}) {        next unless $_->can("click");        next if $name && $_->name ne $name;	next if $_->disabled;	return $_->click($self, @_);    }    Carp::croak("No clickable input with name $name") if $name;    $self->make_request;}=item @kw = $form->formReturns the current setting as a sequence of key/value pairs.  Notethat keys might be repeated, which means that some values might belost if the return values are assigned to a hash.In scalar context this method returns the number of key/value pairsgenerated.=cutsub form{    my $self = shift;    map { $_->form_name_value($self) } @{$self->{'inputs'}};}=item $form->dumpReturns a textual representation of current state of the form.  Mainlyuseful for debugging.  If called in void context, then the dump isprinted on STDERR.=cutsub dump{    my $self = shift;    my $method  = $self->{'method'};    my $uri     = $self->{'action'};    my $enctype = $self->{'enctype'};    my $dump = "$method $uri";    $dump .= " ($enctype)"	if $enctype ne "application/x-www-form-urlencoded";    $dump .= " [$self->{attr}{name}]"    	if exists $self->{attr}{name};    $dump .= "\n";    for ($self->inputs) {	$dump .= "  " . $_->dump . "\n";    }    print STDERR $dump unless defined wantarray;    $dump;}#---------------------------------------------------package HTML::Form::Input;=back=head1 INPUTSAn C<HTML::Form> objects contains a sequence of I<inputs>.  References tothe inputs can be obtained with the $form->inputs or $form->find_inputmethods.Note that there is I<not> a one-to-one correspondence between inputI<objects> and E<lt>inputE<gt> I<elements> in the HTML document.  Aninput object basically represents a name/value pair, so when multipleHTML elements contribute to the same name/value pair in the submittedform they are combined.The input elements that are mapped one-to-one are "text", "textarea","password", "hidden", "file", "image", "submit" and "checkbox".  Forthe "radio" and "option" inputs the story is not as simple: AllE<lt>input type="radio"E<gt> elements with the same name willcontribute to the same input radio object.  The number of radio inputobjects will be the same as the number of distinct names used for theE<lt>input type="radio"E<gt> elements.  For a E<lt>selectE<gt> elementwithout the C<multiple> attribute there will be one input object oftype of "option".  For a E<lt>select multipleE<gt> element there willbe one input object for each contained E<lt>optionE<gt> element.  Eachone of these option objects will have the same name.The following methods are available for the I<input> objects:=over 4=cutsub new{    my $class = shift;    my $self = bless {@_}, $class;    $self;}sub add_to_form{    my($self, $form) = @_;    push(@{$form->{'inputs'}}, $self);    $self;}sub fixup {}=item $input->typeReturns the type of this input.  The type is one of the followingstrings: "text", "password", "hidden", "textarea", "file", "image", "submit","radio", "checkbox" or "option".=cutsub type{    shift->{type};}=item $name = $input->name=item $input->name( $new_name )This method can be used to get/set the current name of the input.=item $value = $input->value=item $input->value( $new_value )This method can be used to get/set the current value of aninput.If the input only can take an enumerated list of values, then it is anerror to try to set it to something else and the method will croak ifyou try.You will also be able to set the value of read-only inputs, but awarning will be generated if running under C<perl -w>.=cutsub name{    my $self = shift;    my $old = $self->{name};    $self->{name} = shift if @_;    $old;}sub value{    my $self = shift;    my $old = $self->{value};    $self->{value} = shift if @_;    $old;}=item $input->possible_valuesReturns a list of all values that an input can take.  For inputs thatdo not have discrete values, this returns an empty list.=cutsub possible_values{    return;}=item $input->other_possible_valuesReturns a list of all values not tried yet.=cutsub other_possible_values{    return;}=item $input->value_namesFor some inputs the values can have names that are different from thevalues themselves.  The number of names returned by this method willmatch the number of values reported by $input->possible_values.When setting values using the value() method it is also possible touse the value names in place of the value itself.=cutsub value_names {    return}=item $bool = $input->readonly=item $input->readonly( $bool )This method is used to get/set the value of the readonly attribute.You are allowed to modify the value of readonly inputs, but settingthe value will generate some noise when warnings are enabled.  Hiddenfields always start out readonly.=cutsub readonly {    my $self = shift;    my $old = $self->{readonly};    $self->{readonly} = shift if @_;    $old;}=item $bool = $input->disabled=item $input->disabled( $bool )This method is used to get/set the value of the disabled attribute.Disabled inputs do not contribute any key/value pairs for the formvalue.=cutsub disabled {    my $self = shift;    my $old = $self->{disabled};    $self->{disabled} = shift if @_;    $old;}=item $input->form_name_valueReturns a (possible empty) list of key/value pairs that should beincorporated in the form value from this input.=cutsub form_name_value{    my $self = shift;    my $name = $self->{'name'};    return unless defined $name;    return if $self->{disabled};    my $value = $self->value;    return unless defined $value;    return ($name => $value);}sub dump{    my $self = shift;    my $name = $self->name;    $name = "<NONAME>" unless defined $name;    my $value = $self->value;    $value = "<UNDEF>" unless defined $value;    my $dump = "$name=$value";    my $type = $self->type;    $type .= " disabled" if $self->disabled;    $type .= " readonly" if $self->readonly;    return sprintf "%-30s %s", $dump, "($type)" unless $self->{menu};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -