Revision history for Perl extension URI::Template
+0.21 2015-01-08
+ - variables() now returns items in their order of appearance (Artem
+ Krivopolenov)
+
0.20 2015-01-05
- Handle "0" as a template properly (RT 101109)
resources:
license: http://dev.perl.org/licenses/
repository: http://github.com/bricas/uri-template
-version: 0.20
+version: 0.21
also set and parse a new template string.
variables
- Returns an array of unique variable names found in the template. NB:
- they are returned in random order.
+ Returns an array of unique variable names found in the template (in the
+ order of appearance).
expansions
This method returns an list of expansions found in the template.
use strict;
use warnings;
-our $VERSION = '0.20';
+our $VERSION = '0.21';
use URI;
use URI::Escape ();
sub _study {
my ( $self ) = @_;
my @hunks = grep { defined && length } split /(\{.+?\})/, $self->template;
+ my $pos = 1;
for ( @hunks ) {
next unless /^\{(.+?)\}$/;
- $_ = $self->_compile_expansion( $1 );
+ $_ = $self->_compile_expansion( $1, $pos++ );
}
$self->{ studied } = \@hunks;
}
sub _compile_expansion {
- my ( $self, $str ) = @_;
+ my ( $self, $str, $pos ) = @_;
my %exp = ( op => '', vars => [], str => $str );
if ( $str =~ /^([+#.\/;?&|!\@])(.+)/ ) {
# remove "optional" flag (for opensearch compatibility)
$var{ name } =~ s{\?$}{};
- $self->{ _vars }->{ $var{ name } }++;
+ $self->{ _vars }->{ $var{ name } } = $pos;
push @{ $exp{ vars } }, \%var;
}
}
sub variables {
- return keys %{ $_[ 0 ]->{ _vars } };
+ return sort {$_[ 0 ]->{ _vars }->{ $a } <=> $_[ 0 ]->{ _vars }->{ $b } } keys %{ $_[ 0 ]->{ _vars } };
}
sub expansions {
=head2 variables
-Returns an array of unique variable names found in the template. NB: they are returned in random order.
+Returns an array of unique variable names found in the template (in the order of appearance).
=head2 expansions
}
{
- my $text = 'http://foo.com/{bar}/{baz}?q=%7B';
+ my $text = 'http://foo.com/{bar}/{baz}?{foo}=%7B&{abr}=1';
my $template = URI::Template->new( $text );
isa_ok( $template, 'URI::Template' );
- is_deeply( [ sort $template->variables ], [ 'bar', 'baz' ], 'variables()' );
+ is_deeply( [ $template->variables ], [ 'bar', 'baz', 'foo', 'abr' ], 'variables() in order of appearance' );
is( "$template", $text, 'stringify' );
{
- my $result = $template->process( bar => 'x', baz => 'y' );
- is( $result, 'http://foo.com/x/y?q=%7B', 'process()' );
+ my $result = $template->process( bar => 'x', baz => 'y', foo => 'b', abr => 'a' );
+ is( $result, 'http://foo.com/x/y?b=%7B&a=1', 'process()' );
isa_ok( $result, 'URI', 'return value from process() isa URI' );
}
{
- my $result = $template->process_to_string( bar => 'x', baz => 'y' );
- is( $result, 'http://foo.com/x/y?q=%7B', 'process_to_string()' );
+ my $result = $template->process_to_string( bar => 'x', baz => 'y', foo => 'b', abr => 'a' );
+ is( $result, 'http://foo.com/x/y?b=%7B&a=1', 'process_to_string()' );
ok( !ref $result, 'result is not a ref' );
}
}