Debian lenny version packages
[pkg-perl] / deb-src / libwww-mechanize-perl / libwww-mechanize-perl-1.34 / lib / WWW / Mechanize / Link.pm
1 package WWW::Mechanize::Link;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 WWW::Mechanize::Link - Link object for WWW::Mechanize
9
10 =head1 SYNOPSIS
11
12 Link object to encapsulate all the stuff that Mech needs but nobody
13 wants to deal with as an array.
14
15 =head1 Constructor
16
17 =head2 new()
18
19     my $link = WWW::Mechanize::Link->new( {
20         url  => $url,
21         text => $text,
22         name => $name,
23         tag  => $tag,
24         base => $base,
25         attr => $attr_href,
26     } );
27
28 For compatibility, this older interface is also supported:
29
30  new( $url, $text, $name, $tag, $base, $attr_href )
31
32 Creates and returns a new C<WWW::Mechanize::Link> object.
33
34 =cut
35
36 sub new {
37     my $class = shift;
38
39     my $self;
40
41     # The order of the first four must stay as they are for
42     # compatibility with older code.
43     if ( ref $_[0] eq 'HASH' ) {
44         $self = [ @{$_[0]}{ qw( url text name tag base attrs ) } ];
45     }
46     else {
47         $self = [ @_ ];
48     }
49
50     return bless $self, $class;
51 }
52
53 =head1 Accessors
54
55 =head2 $link->url()
56
57 URL from the link
58
59 =head2 $link->text()
60
61 Text of the link
62
63 =head2 $link->name()
64
65 NAME attribute from the source tag, if any.
66
67 =head2 $link->tag()
68
69 Tag name (one of: "a", "area", "frame", "iframe" or "meta").
70
71 =head2 $link->base()
72
73 Base URL to which the links are relative.
74
75 =head2 $link->attrs()
76
77 Returns hash ref of all the attributes and attribute values in the tag. 
78
79 =cut
80
81 sub url   { return ($_[0])->[0]; }
82 sub text  { return ($_[0])->[1]; }
83 sub name  { return ($_[0])->[2]; }
84 sub tag   { return ($_[0])->[3]; }
85 sub base  { return ($_[0])->[4]; }
86 sub attrs { return ($_[0])->[5]; }
87
88 =head2 $link->URI()
89
90 Returns the URL as a L<URI::URL> object.
91
92 =cut
93
94 sub URI {
95     my $self = shift;
96
97     require URI::URL;
98     my $URI = URI::URL->new( $self->url, $self->base );
99
100     return $URI;
101 }
102
103 =head2 $link->url_abs()
104
105 Returns a URI::URL object for the absolute form of the string.
106
107 =cut
108
109 sub url_abs {
110     my $self = shift;
111
112     return $self->URI->abs;
113 }
114
115 =head1 COPYRIGHT
116
117 Copyright (c) 2004-2007 Andy Lester. All rights reserved. This program is
118 free software; you can redistribute it and/or modify it under the same
119 terms as Perl itself.
120
121 =cut
122
123 # vi:et:sw=4 ts=4
124
125 1;