use strict;
use warnings;
-our $VERSION = '0.17';
+our $VERSION = '0.19';
use URI;
use URI::Escape ();
sub new {
my $class = shift;
- my $templ = shift || die 'No template provided';
+ my $templ = shift || '';
my $self = bless { template => $templ, _vars => {} } => $class;
$self->_study;
$join = '&' if $exp->{ op } =~ /[?&]/;
if ( ref $value eq 'ARRAY' ) {
- if( !@$value ) {
- return if $var->{ explode };
- return $var->{ name } . '=';
- }
+ return if !@$value;
if ( $var->{ explode } ) {
return join( $join,
map { $var->{ name } . '=' . _quote( $_, $safe ) } @$value );
}
}
elsif ( ref $value eq 'HASH' ) {
- if( !keys %$value ) {
- return if $var->{ explode };
- return $var->{ name } . '=';
- }
+ return if !keys %$value;
if ( $var->{ explode } ) {
return join(
$join,
}
sub template {
- return $_[ 0 ]->{ template };
+ my $self = shift;
+ my $new_template = shift;
+
+ # Update template
+ if ( $new_template && $new_template ne $self->{ template } ) {
+ $self->{ template } = $new_template;
+ $self->{ _vars } = {};
+ $self->_study;
+ return $self;
+ }
+
+ return $self->{ template };
}
sub variables {
=head1 SYNOPSIS
use URI::Template;
+
my $template = URI::Template->new( 'http://example.com/{x}' );
my $uri = $template->process( x => 'y' );
+
+ # or
+
+ my $template = URI::Template->new();
+ $template->template( 'http://example.com/{x}' );
+ my $uri = $template->process( x => 'y' );
+
# uri is a URI object with value 'http://example.com/y'
=head1 DESCRIPTION
This module provides a wrapper around URI templates as described in RFC 6570:
-http://tools.ietf.org/html/rfc6570
+L<< http://tools.ietf.org/html/rfc6570 >>.
=head1 INSTALLATION
=head2 new( $template )
Creates a new L<URI::Template> instance with the template passed in
-as the first parameter.
+as the first parameter (optional).
-=head2 template
+=head2 template( $template )
-This method returns the original template string.
+This method returns the original template string. If provided, it will also set and parse a
+new template string.
=head2 variables
=head1 COPYRIGHT AND LICENSE
-Copyright 2007-2013 by Brian Cassidy
+Copyright 2007-2015 by Brian Cassidy
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.