Commits

shl...@52c325ad-5fd0-0310-8a0f-c43feede02cc  committed a12b7af

Implemented the %(name)s conversion.

  • Participants
  • Parent commits 4f5bbdb

Comments (0)

Files changed (2)

File modules/Text-Sprintf-Named/lib/Text/Sprintf/Named.pm

 {
     my $self = shift;
 
-    my $args = shift || { args => {}};
+    my $args = shift || {};
+
+    my $named_params = $args->{args} || {};
 
     my $format = $self->_fmt;
 
-    $format =~ s{%%}{%}g;
+    $format =~ s/%(%|\(([a-zA-Z_]\w*)\)(s))/
+        $self->_conversion({
+            format_args => $args,
+            named_params => $named_params,
+            conv => $1,
+            name => $2,
+            conv_raw => $3,
+        })
+        /ge;
 
     return $format;
 }
 
+sub _conversion
+{
+    my ($self, $args) = @_;
+
+    if ($args->{conv} eq "%")
+    {
+        return "%";
+    }
+    else
+    {
+        return sprintf(("%" . $args->{conv_raw}),
+            $args->{named_params}->{$args->{name}}
+        );
+    }
+}
+
 =head1 EXPORT
 
 A list of functions that can be exported.  You can delete this section

File modules/Text-Sprintf-Named/t/01-use.t

 use strict;
 use warnings;
 
-use Test::More tests => 6;
+use Test::More tests => 7;
 
 use Text::Sprintf::Named;
 
         "Testing multiple consecutive % sign"
     );   
 }
+
+{
+    # TEST
+    is (n_s("Hello %(name)s!", {name => "Tim"}),
+        "Hello Tim!",
+        "%(name)s conversion"
+    );
+}