File: URI.pm

package info (click to toggle)
libbadger-perl 0.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,400 kB
  • sloc: perl: 11,004; makefile: 9
file content (139 lines) | stat: -rw-r--r-- 2,444 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#========================================================================
#
# Badger::Codec::URI
#
# DESCRIPTION
#   Codec module for URI encoding/decoding 
#
# AUTHOR
#   Andy Wardley   <abw@wardley.org>
#
#========================================================================

package Badger::Codec::URI;

use Badger::Class
    version => 0.01,
    base    => 'Badger::Codec';

# cache of escaped characters
our $URI_ESCAPES;

sub encode_uri {
    my $uri = shift;

    utf8::encode($uri)
        if $] >= 5.008 && utf8::is_utf8($uri);

    $URI_ESCAPES ||= {
        map { ( chr($_), sprintf("%%%02X", $_) ) } 
        (0..255)
    };
  
    $uri =~ s/([^A-Za-z0-9\-_.!~*'()])/$URI_ESCAPES->{$1}/eg;
    $uri;
}

sub decode_uri {
    my $uri = shift;
    $uri =~ tr/+/ /;
    $uri =~ s/%([0-9a-fA-F]{2})/pack("c", hex($1))/ge;
    $uri;
}

sub encode {
    shift;
    goto &encode_uri;
}

sub decode {
    shift;
    goto &decode_uri;
}

sub encoder {
    \&encode_uri;
}

sub decoder {
    \&decode_uri;
}


1;


__END__

=head1 NAME

Badger::Codec::URI - URI encode/decode 

=head1 SYNOPSIS

    use Badger::Codec::URI;
    my $codec   = Badger::Codec::URI->new();
    my $encoded = $codec->encode("Hello World!");
    my $decoded = $codec->decode($encoded);

=head1 DESCRIPTION

This module implements a subclass of L<Badger::Codec> for
URI encoding and decoding.

=head1 FUNCTIONS

=head2 encode_uri($data)

This function URI-encodes the C<$data> passed as an argument.

=head2 decode_uri($data)

This function URI-decodes the C<$data> passed as an argument.

=head1 METHODS

=head2 encode($data)

This method URI-encodes the data referenced by the first argument.
It delegates to the L<encode_uri()> function.

    $encoded = Badger::Codec::URI->encode($data);   

=head2 decode($data)

This method decodes the encoded data passed as the first argument.
It delegates to the L<decode_uri()> function.

    $decoded = Badger::Codec::URI->decode($encoded);

=head2 encoder()

This method returns a reference to the L<encode_uri()> function.

=head2 decoder()

This method returns a reference to the L<decode_uri()> function.

=head1 AUTHOR

Andy Wardley L<http://wardley.org/>

=head1 COPYRIGHT

Copyright (C) 2008-2009 Andy Wardley. All rights reserved.

=head1 SEE ALSO

L<Badger::Codecs>, L<Badger::Codec>

=cut

# Local Variables:
# mode: Perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4: