10 use overload '""' => \&as_string;
14 URI::Template - Object for handling URI templates
19 my $template = URI::Template->new( 'http://example.com/{x}' );
20 my $uri = $template->process( x => 'y' );
21 # uri is a URI object with value 'http://example.com/y'
23 my %result = $template->deparse( $uri );
24 # %result is ( x => 'y' )
28 This is an initial attempt to provide a wrapper around URI templates
29 as described at http://www.ietf.org/internet-drafts/draft-gregorio-uritemplate-00.txt
33 To install this module via Module::Build:
36 ./Build # or `perl Build`
37 ./Build test # or `perl Build test`
38 ./Build install # or `perl Build install`
40 To install this module via ExtUtils::MakeMaker:
49 =head2 new( $template )
51 Creates a new L<URI::Template> instance with the template passed in
52 as the first parameter.
58 my $templ = shift || die 'No template provided';
59 my $self = bless { template => $templ }, $class;
66 Returns the original template string. Also used when the object is
72 return $_[ 0 ]->{ template };
77 Returns an array of variable names found in the template. NB: they
78 are returned in random order.
84 my %vars = map { $_ => 1 } $self->all_variables;
88 =head2 all_variables( )
90 Returns an array of variable names found as they appear in template --
91 in order, duplicates included.
97 my @vars = $self->as_string =~ /{(.+?)}/g;
101 =head2 process( %vars|\@values )
103 Given a list of key-value pairs or an array ref of values (for
104 positional substitution), it will URI escape the values and
105 substitute them in to the template. Returns a URI object.
111 return URI->new( $self->process_to_string( @_ ) );
114 =head2 process_to_string( %vars|\@values )
116 Processes input like the C<process> method, but doesn't
117 inflate the result to a URI object.
121 sub process_to_string {
125 return $self->_process_by_position( @_ );
128 return $self->_process_by_key( @_ );
132 sub _process_by_key {
134 my @vars = $self->variables;
136 my $uri = $self->as_string;
139 for my $var ( @vars ) {
140 $params{ $var } = defined $params{ $var }
141 ? URI::Escape::uri_escape( $params{ $var } )
145 my $regex = '\{(' . join( '|', map quotemeta, @vars ) . ')\}';
146 $uri =~ s/$regex/$params{$1}/eg;
151 sub _process_by_position {
153 my @params = @{ $_[ 0 ] };
155 my $uri = $self->as_string;
157 $uri =~ s/{(.+?)}/@params
158 ? defined $params[ 0 ]
159 ? URI::Escape::uri_escape( shift @params )
166 =head2 deparse( $uri )
168 Does some rudimentary deparsing of a uri based on the current template.
169 Returns a hash with the extracted values.
177 if( !$self->{ deparse_re } ) {
178 my $templ = $self->as_string;
179 $self->{ vars_list } = [ $templ =~ /{(.+?)}/g ];
180 $templ =~ s/{.+?}/(.+?)/g;
181 $self->{ deparse_re } = qr/$templ/;
184 my @matches = $uri =~ $self->{ deparse_re };
187 @results{ @{ $self->{ vars_list } } } = @matches;
195 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
199 =head1 COPYRIGHT AND LICENSE
201 Copyright 2007 by Brian Cassidy
203 This library is free software; you can redistribute it and/or modify
204 it under the same terms as Perl itself.