| 1 | package Any::Renderer::UrlEncoded; | ||
| 2 | |||
| 3 | # $Id: UrlEncoded.pm,v 1.11 2006/09/04 12:15:53 johna Exp $ | ||
| 4 | |||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 0 | ▶ | use Hash::Flatten; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use URI::Escape; | ||
| 10 | |||
| 11 | $VERSION = sprintf"%d.%03d", q$Revision: 1.11 $ =~ /: (\d+)\.(\d+)/; | ||
| 12 | |||
| 13 | use constant FORMAT_NAME => "UrlEncoded"; | ||
| 14 | |||
| 15 | sub new | ||
| 16 | { | ||
| 17 | my ( $class, $format, $options ) = @_; | ||
| 18 | die("Invalid format $format") unless($format eq FORMAT_NAME); | ||
| 19 | |||
| 20 | $options ||= {}; | ||
| 21 | my $self = { | ||
| 22 | 'options' => $options, | ||
| 23 | 'delim' => $options->{Delimiter} || '&', | ||
| 24 | }; | ||
| 25 | |||
| 26 | bless $self, $class; | ||
| 27 | return $self; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub render | ||
| 31 | { | ||
| 32 | my ( $self, $data ) = @_; | ||
| 33 | TRACE ( "Rendering data as UrlEncoded" ); | ||
| 34 | |||
| 35 | my $flat = Hash::Flatten::flatten ( $data, $self->{options}{FlattenOptions} ); | ||
| 36 | DUMP ( $flat ); | ||
| 37 | |||
| 38 | my $rv = join ( $self->{delim}, map { URI::Escape::uri_escape($_) . "=" . URI::Escape::uri_escape($flat->{$_}) } keys %$flat ); | ||
| 39 | TRACE($rv); | ||
| 40 | |||
| 41 | return $rv; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub requires_template | ||
| 45 | { | ||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub available_formats | ||
| 50 | { | ||
| 51 | return [ FORMAT_NAME ]; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub TRACE {} | ||
| 55 | sub DUMP {} | ||
| 56 | |||
| 57 | 1; | ||
| 58 | |||
| 59 | =head1 NAME | ||
| 60 | |||
| 61 | Any::Renderer::UrlEncoded - convert data structures into a UrlEncoded string | ||
| 62 | |||
| 63 | =head1 SYNOPSIS | ||
| 64 | |||
| 65 | use Any::Renderer; | ||
| 66 | |||
| 67 | my %options = ('FlattenOptions' => {'HashDelimiter' => '->'}); | ||
| 68 | my $format = "UrlEncoded"; | ||
| 69 | my $r = new Any::Renderer ( $format, \%options ); | ||
| 70 | |||
| 71 | my $data_structure = {...}; | ||
| 72 | my $string = $r->render ( $data_structure ); | ||
| 73 | |||
| 74 | You can get a list of all formats that this module handles using the following syntax: | ||
| 75 | |||
| 76 | my $list_ref = Any::Renderer::UrlEncoded::available_formats (); | ||
| 77 | |||
| 78 | Also, determine whether or not a format requires a template with requires_template: | ||
| 79 | |||
| 80 | my $bool = Any::Renderer::UrlEncoded::requires_template ( $format ); | ||
| 81 | |||
| 82 | =head1 DESCRIPTION | ||
| 83 | |||
| 84 | Any::Renderer::UrlEncoded renders a Perl data structure as a URI encoded string. | ||
| 85 | Keys and values are escaped via URI::Escape::uri_escape. For example: | ||
| 86 | |||
| 87 | perl -MAny::Renderer -e "print Any::Renderer->new('UrlEncoded')->render({a => 1, b => [2,3]})" | ||
| 88 | |||
| 89 | results in: | ||
| 90 | |||
| 91 | a=1&b%3A1=3&b%3A0=2 | ||
| 92 | |||
| 93 | This can be passed as a query string to a CGI script and reconstituted using Hash::Flatten::unflatten: | ||
| 94 | |||
| 95 | use CGI; | ||
| 96 | use Hash::Flatten; | ||
| 97 | my $data_structure = Hash::Flatten::unflatten( CGI->new()->Vars() ); | ||
| 98 | |||
| 99 | B<NB. the top-level of the data structure must be a hashref.> | ||
| 100 | |||
| 101 | =head1 FORMATS | ||
| 102 | |||
| 103 | =over 4 | ||
| 104 | |||
| 105 | =item UrlEncoded | ||
| 106 | |||
| 107 | =back | ||
| 108 | |||
| 109 | =head1 METHODS | ||
| 110 | |||
| 111 | =over 4 | ||
| 112 | |||
| 113 | =item $r = new Any::Renderer::UrlEncoded($format,\%options) | ||
| 114 | |||
| 115 | C<$format> must be C<UrlEncoded>. | ||
| 116 | See L</OPTIONS> for a description of valid C<%options>. | ||
| 117 | |||
| 118 | =item $string = $r->render($data_structure) | ||
| 119 | |||
| 120 | The main method. | ||
| 121 | |||
| 122 | =item $bool = Any::Renderer::UrlEncoded::requires_template($format) | ||
| 123 | |||
| 124 | False in this case. | ||
| 125 | |||
| 126 | =item $list_ref = Any::Renderer::UrlEncoded::available_formats() | ||
| 127 | |||
| 128 | Just the one - C<UrlEncoded>. | ||
| 129 | |||
| 130 | =back | ||
| 131 | |||
| 132 | =head1 OPTIONS | ||
| 133 | |||
| 134 | =over 4 | ||
| 135 | |||
| 136 | =item Delimiter | ||
| 137 | |||
| 138 | The character separating each key=value pair. Defaults to &. You might want to change to ; if you are embedding values in XML documents. | ||
| 139 | |||
| 140 | =item FlattenOptions | ||
| 141 | |||
| 142 | A hashref passed to Hash::Flatten (see L<Hash::Flatten> for the list of options it supports). | ||
| 143 | |||
| 144 | =back | ||
| 145 | |||
| 146 | =head1 SEE ALSO | ||
| 147 | |||
| 148 | L<Hash::Flatten>, L<URI::Escape>, L<Any::Renderer> | ||
| 149 | |||
| 150 | =head1 VERSION | ||
| 151 | |||
| 152 | $Revision: 1.11 $ on $Date: 2006/09/04 12:15:53 $ by $Author: johna $ | ||
| 153 | |||
| 154 | =head1 AUTHOR | ||
| 155 | |||
| 156 | Matt Wilson and John Alden <cpan _at_ bbc _dot_ co _dot_ uk> | ||
| 157 | |||
| 158 | =head1 COPYRIGHT | ||
| 159 | |||
| 160 | (c) BBC 2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL. | ||
| 161 | |||
| 162 | See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt | ||
| 163 | |||
| 164 | =cut | ||