Commits

Anonymous committed 073c9b0

Added very basic functionality - just converting "%%" to "%".

  • Participants
  • Parent commits 024efd0

Comments (0)

Files changed (3)

File modules/Text-Sprintf-Named/MANIFEST

 README
 lib/Text/Sprintf/Named.pm
 t/00-load.t
+t/01-use.t
 t/boilerplate.t
 t/pod-coverage.t
 t/pod.t

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

 use warnings;
 use strict;
 
+use Carp;
+
 =head1 NAME
 
 Text::Sprintf::Named - sprintf-like function with named conversions
 
 =head1 FUNCTIONS
 
+=head2 my $formatter = Text::Sprintf::Named->new({fmt => $format})
+
+Creates a new object which formats according to the C<$format> format.
+
+=cut
+
+sub new
+{
+    my $class = shift;
+
+    my $self = {};
+    bless $self, $class;
+
+    $self->_init(@_);
+
+    return $self;
+}
+
+sub _init
+{
+    my ($self, $args) = @_;
+
+    my $fmt = $args->{fmt} or
+        confess "The 'fmt format was not specified for Text::Sprintf::Named.";
+    $self->_fmt($fmt);
+
+    return 0;
+}
+
+sub _fmt
+{
+    my $self = shift;
+
+    if (@_)
+    {
+        $self->{_fmt} = shift;
+    }
+
+    return $self->{_fmt};
+}
+
+=head2 $formatter->format({args => \%bindings})
+
+Returns the formatting string as formatted using the named parameters
+pointed to by C<args>.
+
+=cut
+
+sub format
+{
+    my $self = shift;
+
+    my $args = shift || { args => {}};
+
+    my $format = $self->_fmt;
+
+    $format =~ s{%%}{%}g;
+
+    return $format;
+}
+
 =head1 EXPORT
 
 A list of functions that can be exported.  You can delete this section

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

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 4;
+
+use Text::Sprintf::Named;
+
+# Instantiate an object.
+{
+    my $obj = Text::Sprintf::Named->new({fmt => "Hello %(name)s!"});
+
+    # TEST
+    ok ($obj, "Object was instantiated");
+}
+
+{
+    my $obj = Text::Sprintf::Named->new({fmt => "95%% Humidity"});
+
+    # TEST
+    is ($obj->format(), "95% Humidity", "Checking ->format() - 1");
+    # TEST
+    is ($obj->format({args => {}}), "95% Humidity",
+        "Checking ->format() - 2 - empty hashref"
+    );
+    # TEST
+    is ($obj->format({args => {foo => "Bardom", "isaac" => "Newton"}}), 
+        "95% Humidity",
+        "Checking ->format() - 3 - full hashref"
+    );
+}
+
+# TODO : test for:
+# 1. Consecutive percent signs.
+# 2. More than one double percent sign in a string.