Skip to content

Commit 8e31be3

Browse files
simbabqueoalders
authored andcommitted
clarify documentation for select() #77
This also modernises the test slightly.
1 parent 6258503 commit 8e31be3

File tree

3 files changed

+64
-16
lines changed

3 files changed

+64
-16
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Revision history for WWW::Mechanize
88
- Raise minimum Perl to 5.8 to match what we test, what dependencies
99
depend on, etc. (GH#352) (James Raspass)
1010
[DOCUMENTATION]
11+
- Clarify documentation for select() (GH#77) (Julien Fiegehenn)
1112
- Various POD fixes (Julien Fiegehenn)
1213
[TESTS]
1314
- Test that follow_link(n=> 'all') warns (Kueppo Tcheukam)

lib/WWW/Mechanize.pm

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,23 +1977,61 @@ sub field {
19771977
}
19781978
}
19791979

1980-
=head2 $mech->select($name, $value)
1980+
=head2 $mech->select($name, $new_or_additional_single_value)
19811981
1982-
=head2 $mech->select($name, \@values)
1982+
=head2 $mech->select($name, \%new_single_value_by_number)
1983+
1984+
=head2 $mech->select($name, \@new_list_of_values)
1985+
1986+
=head2 $mech->select($name, \%new_list_of_values_by_number)
19831987
19841988
Given the name of a C<select> field, set its value to the value
1985-
specified. If the field is not C<< <select multiple> >> and the
1986-
C<$value> is an array, only the B<first> value will be set. [Note:
1987-
the documentation previously claimed that only the last value would
1988-
be set, but this was incorrect.] Passing C<$value> as a hash with
1989-
an C<n> key selects an item by number (e.g.
1990-
C<< {n => 3} >> or C<< {n => [2,4]} >>).
1989+
specified.
1990+
1991+
# select 'foo'
1992+
$mech->select($name, 'foo');
1993+
1994+
If the field is not C<< <select multiple> >> and the
1995+
C<$value> is an array reference, only the B<first> value will be set. [Note:
1996+
until version 1.05_03 the documentation claimed that only the last value would
1997+
be set, but this was incorrect.]
1998+
1999+
# select 'bar'
2000+
$mech->select($name, ['bar', 'ignored', 'ignored']);
2001+
2002+
Passing C<$value> as a hash reference with an C<n> key selects an item by number.
2003+
2004+
# select the third value
2005+
$mech->select($name, {n => 3});
2006+
19912007
The numbering starts at 1. This applies to the current form.
19922008
19932009
If you have a field with C<< <select multiple> >> and you pass a single
19942010
C<$value>, then C<$value> will be added to the list of fields selected,
1995-
without clearing the others. However, if you pass an array reference,
1996-
then all previously selected values will be cleared.
2011+
without clearing the others.
2012+
2013+
# add 'bar' to the list of selected values
2014+
$mech->select($name, 'bar');
2015+
2016+
However, if you pass an array reference, then all previously selected values
2017+
will be cleared and replaced with all values inside the array reference.
2018+
2019+
# replace the selection with 'foo' and 'bar'
2020+
$mech->select($name, ['foo', 'bar']);
2021+
2022+
This also works when selecting by numbers, in which case the value of the C<n>
2023+
key will be an array reference of value numbers you want to replace the
2024+
selection with.
2025+
2026+
# replace the selection with the 2nd and 4th element
2027+
$mech->select($name, {n => [2, 4]});
2028+
2029+
To add multiple additional values to the list of selected fields without
2030+
clearing, call C<select> in the simple C<$value> form with each single value
2031+
in a loop.
2032+
2033+
# add all values in the array to the selection
2034+
$mech->select($name, $_) for @additional_values;
19972035
19982036
Returns true on successfully setting the value. On failure, returns
19992037
false and calls C<< $self->warn() >> with an error message.

t/select.t

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
use warnings;
44
use strict;
5-
use Test::More tests => 14;
5+
use Test::More;
6+
use Test::Warn qw( warning_like );
67
use URI::file ();
78

89
BEGIN {
@@ -32,19 +33,23 @@ $form = $mech->current_form();
3233
# Multi-select
3334

3435
# pass multiple values to a multi select
36+
$form->param('multilist', undef);
3537
$mech->select('multilist', \@sendmulti);
3638
@return = $form->param('multilist');
3739
is_deeply(\@return, \@sendmulti, 'multi->multi value is ' . join(' ', @return));
3840

41+
$form->param('multilist', undef);
3942
$mech->select('multilist', \%sendmulti);
4043
@return = $form->param('multilist');
4144
is_deeply(\@return, \@sendmulti, 'multi->multi value is ' . join(' ', @return));
4245

4346
# pass a single value to a multi select
47+
$form->param('multilist', undef);
4448
$mech->select('multilist', $sendsingle);
4549
$return = $form->param('multilist');
4650
is($return, $sendsingle, "single->multi value is '$return'");
4751

52+
$form->param('multilist', undef);
4853
$mech->select('multilist', \%sendsingle);
4954
$return = $form->param('multilist');
5055
is($return, $sendsingle, "single->multi value is '$return'");
@@ -53,29 +58,33 @@ is($return, $sendsingle, "single->multi value is '$return'");
5358
# Single select
5459

5560
# pass multiple values to a single select (only the _first_ should be set)
61+
$form->param('singlelist', undef);
5662
$mech->select('singlelist', \@sendmulti);
5763
@return = $form->param('singlelist');
5864
is_deeply(\@return, \@singlereturn, 'multi->single value is ' . join(' ', @return));
5965

66+
$form->param('singlelist', undef);
6067
$mech->select('singlelist', \%sendmulti);
6168
@return = $form->param('singlelist');
6269
is_deeply(\@return, \@singlereturn, 'multi->single value is ' . join(' ', @return));
6370

6471

6572
# pass a single value to a single select
73+
$form->param('singlelist', undef);
6674
$rv = $mech->select('singlelist', $sendsingle);
6775
$return = $form->param('singlelist');
6876
is($return, $sendsingle, "single->single value is '$return'");
6977

78+
$form->param('singlelist', undef);
7079
$rv = $mech->select('singlelist', \%sendsingle);
7180
$return = $form->param('singlelist');
7281
is($return, $sendsingle, "single->single value is '$return'");
7382

7483
# test return value from $mech->select
7584
is($rv, 1, 'return 1 after successful select');
7685

77-
EAT_THE_WARNING: { # Mech complains about the non-existent field
78-
local $SIG{__WARN__} = sub {};
79-
$rv = $mech->select('missing_list', 1);
80-
}
81-
is($rv, undef, 'return undef after failed select');
86+
warning_like { $rv = $mech->select( 'missing_list', 1 ) } qr/not found/,
87+
'warning when field is not found';
88+
is( $rv, undef, 'return undef after failed select' );
89+
90+
done_testing;

0 commit comments

Comments
 (0)