6 our $VERSION = '0.08_01';
10 use overload '""' => \&as_string;
12 my $unsafe = q(^A-Za-z0-9\-_.~!\$\&'()*+,;=:/?\[\]#@);
16 URI::Template - Object for handling URI templates
21 my $template = URI::Template->new( 'http://example.com/{x}' );
22 my $uri = $template->process( x => 'y' );
23 # uri is a URI object with value 'http://example.com/y'
25 my %result = $template->deparse( $uri );
26 # %result is ( x => 'y' )
30 This is an initial attempt to provide a wrapper around URI templates
31 as described at http://www.ietf.org/internet-drafts/draft-gregorio-uritemplate-01.txt
35 To install this module via Module::Build:
38 ./Build # or `perl Build`
39 ./Build test # or `perl Build test`
40 ./Build install # or `perl Build install`
42 To install this module via ExtUtils::MakeMaker:
51 =head2 new( $template )
53 Creates a new L<URI::Template> instance with the template passed in
54 as the first parameter.
60 my $templ = shift || die 'No template provided';
61 my $self = bless { template => $templ }, $class;
68 Returns the original template string. Also used when the object is
74 return $_[ 0 ]->{ template };
79 Returns an array of variable names found in the template. NB: they
80 are returned in random order.
86 my %vars = map { $_ => 1 } $self->all_variables;
90 =head2 all_variables( )
92 Returns an array of variable names found as they appear in template --
93 in order, duplicates included.
99 my @vars = $self->as_string =~ /{(.+?)}/g;
103 =head2 process( %vars|\@values )
105 Given a list of key-value pairs or an array ref of values (for
106 positional substitution), it will URI escape the values and
107 substitute them in to the template. Returns a URI object.
113 return URI->new( $self->process_to_string( @_ ) );
116 =head2 process_to_string( %vars|\@values )
118 Processes input like the C<process> method, but doesn't
119 inflate the result to a URI object.
123 sub process_to_string {
127 return $self->_process_by_position( @_ );
130 return $self->_process_by_key( @_ );
134 sub _process_by_key {
136 my @vars = $self->variables;
138 my $uri = $self->as_string;
141 for my $var ( @vars ) {
142 $params{ $var } = defined $params{ $var }
143 ? URI::Escape::uri_escape( $params{ $var }, $unsafe )
147 my $regex = '\{(' . join( '|', map quotemeta, @vars ) . ')\}';
148 $uri =~ s/$regex/$params{$1}/eg;
153 sub _process_by_position {
155 my @params = @{ $_[ 0 ] };
157 my $uri = $self->as_string;
159 $uri =~ s/{(.+?)}/@params
160 ? defined $params[ 0 ]
161 ? URI::Escape::uri_escape( shift @params, $unsafe )
168 =head2 deparse( $uri )
170 Does some rudimentary deparsing of a uri based on the current template.
171 Returns a hash with the extracted values.
179 if( !$self->{ deparse_re } ) {
180 my $templ = $self->as_string;
181 $self->{ vars_list } = [ $templ =~ /{(.+?)}/g ];
182 $templ =~ s/{.+?}/(.+?)/g;
183 $self->{ deparse_re } = qr/$templ/;
186 my @matches = $uri =~ $self->{ deparse_re };
189 @results{ @{ $self->{ vars_list } } } = @matches;
197 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
201 =head1 COPYRIGHT AND LICENSE
203 Copyright 2007 by Brian Cassidy
205 This library is free software; you can redistribute it and/or modify
206 it under the same terms as Perl itself.