Subject: | Allow follow() to match again other link attributes. |
From the POD...
$agent->follow($string|$num)
Follow a link. If you provide a string,
the first link whose text matches that
string will be followed.
Text associated with links is not always unique, so it would be useful (to me, anyway ;) if follow() could be made to match $string against other link attributes such as the href or name, as something like...
$agent->follow($string, LINK_HREF);
...would be much more preferrable to the current workaround of rolling my own version of follow()...
LINK:
foreach my $link ( @{ $agent->links } )
{
if ( $link[LINK_HREF] =~ /$string/ )
{
$agent->get( $link[LINK_HREF] );
last LINK;
}
}
A proof of concept patch against 0.36 is attatched, although it's limiting to a single attribute, where opting for a hashref (eg, $agent->follow($string|$num|$hashref)) instead could allow for chained matching.
eg,
$agent->follow( { text => 'Download', href => 'zip' } );
--- Mechanize.pm.orig Wed Jan 22 18:53:11 2003
+++ Mechanize.pm Tue Feb 11 15:19:05 2003
@@ -34,6 +34,8 @@
=cut
+require 5.006; # our() not available < 5.006
+
use strict;
use warnings;
@@ -45,8 +47,22 @@
use Carp;
use URI::URL;
-our @ISA = qw( LWP::UserAgent );
+use constant LINK_HREF => 0;
+use constant LINK_TEXT => 1;
+use constant LINK_NAME => 2;
+require Exporter;
+
+our @ISA = qw( Exporter LWP::UserAgent );
+
+our %EXPORT_TAGS = ( 'attributes' => [ qw(
+ LINK_HREF
+ LINK_TEXT
+ LINK_NAME
+) ] );
+our @EXPORT_OK = ( @{ $EXPORT_TAGS{'attributes'} } );
+our @EXPORT = ( );
+
=head1 VERSION
Version 0.35
@@ -206,9 +222,14 @@
=cut
sub follow {
- my ($self, $link) = @_;
+ my ($self, $link, $attribute) = @_;
my @links = @{$self->{links}};
my $thislink;
+
+ unless ( defined($attribute) && $attribute =~ /^\d+$/ ) {
+ $attribute = LINK_TEXT;
+ }
+
if ( $link =~ /^\d+$/ ) { # is a number?
if ($link <= $#links) {
$thislink = $links[$link];
@@ -219,7 +240,7 @@
}
} else { # user provided a regexp
LINK: foreach my $l (@links) {
- if ($l->[1] =~ /$link/) {
+ if ($l->[$attribute] =~ /$link/) {
$thislink = $l; # grab first match
last LINK;
}
@@ -231,7 +252,7 @@
}
}
- $thislink = $thislink->[0]; # we just want the URL, not the text
+ $thislink = $thislink->[LINK_HREF]; # we just want the URL, not the text
$self->_push_page_stack();
$self->get( $thislink );