line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id$ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
WWW::Shorten::Qwer - Perl interface to qwer.org |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use WWW::Shorten::Qwer; |
10
|
|
|
|
|
|
|
use WWW::Shorten 'Qwer'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$short_url = makeashorterlink($long_url, $short_code); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$long_url = makealongerlink($short_url); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 DESCRIPTION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
A Perl interface to the web site qwer.org. Qwer simply maintains |
19
|
|
|
|
|
|
|
a database of long URLs, each of which has a unique identifier. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=cut |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
package WWW::Shorten::Qwer; |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
1
|
|
21089
|
use 5.006; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
36
|
|
26
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
28
|
|
27
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
39
|
|
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
1
|
|
4
|
use base qw( WWW::Shorten::generic Exporter ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
698
|
|
30
|
|
|
|
|
|
|
our @EXPORT = qw( makeashorterlink makealongerlink ); |
31
|
|
|
|
|
|
|
our $VERSION = '1.00'; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
use Carp; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 Functions |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head2 makeashorterlink |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
The function C will call the Qwer web site passing |
40
|
|
|
|
|
|
|
it your long URL and a mandatory short code. It will return the shorter |
41
|
|
|
|
|
|
|
Qwer version of the URL. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub makeashorterlink ($$) |
46
|
|
|
|
|
|
|
{ |
47
|
|
|
|
|
|
|
my $url = shift or croak 'No URL passed to makeashorterlink'; |
48
|
|
|
|
|
|
|
my $code = shift or croak 'No short code passed to makeashorterlink'; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my $ua = __PACKAGE__->ua(); |
51
|
|
|
|
|
|
|
my $qwer = 'http://qwer.org/submit'; |
52
|
|
|
|
|
|
|
my $resp = $ua->post($qwer, [ |
53
|
|
|
|
|
|
|
name => $code, |
54
|
|
|
|
|
|
|
data => $url, |
55
|
|
|
|
|
|
|
]); |
56
|
|
|
|
|
|
|
return unless $resp->is_redirect; |
57
|
|
|
|
|
|
|
my $content = $resp->content; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
if (my $redir = $resp->header('Location')) { |
60
|
|
|
|
|
|
|
return $redir; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 makealongerlink |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
The function C does the reverse. C |
69
|
|
|
|
|
|
|
will accept as an argument either the full Qwer URL or just the |
70
|
|
|
|
|
|
|
Qwer identifier. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
If anything goes wrong, then either function will return C. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub makealongerlink ($) |
77
|
|
|
|
|
|
|
{ |
78
|
|
|
|
|
|
|
my $qwer_url = shift |
79
|
|
|
|
|
|
|
or croak 'No Qwer key / URL passed to makealongerlink'; |
80
|
|
|
|
|
|
|
my $ua = __PACKAGE__->ua(); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$qwer_url = "http://qwer.org/$qwer_url" |
83
|
|
|
|
|
|
|
unless $qwer_url =~ m!^http://!i; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $resp = $ua->get($qwer_url); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
return unless $resp->is_success; |
88
|
|
|
|
|
|
|
my $content = $resp->content; |
89
|
|
|
|
|
|
|
my ($url) = $content =~ m|^(\w+://.*) |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
return $url; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__END__ |