Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbs committed Oct 14, 2008
0 parents commit 8260231
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Revision history for String-Flogger

1.000 2008-10-13
first release
17 changes: 17 additions & 0 deletions dist.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
dist = String-Flogger
version = 1.000
author = Ricardo SIGNES <rjbs@cpan.org>
license = Perl_5
copyright_holder = Ricardo SIGNES <rjbs@cpan.org>

[Prereq]
JSON = 0
Params::Util = 0
Scalar::Util = 0
Sub::Exporter = 0

[@Filter]
bundle = @Classic
remove = PodVersion

[PodWeaver]
85 changes: 85 additions & 0 deletions lib/String/Flogger.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use strict;
use warnings;
package String::Flogger;
# ABSTRACT - string munging for loggers

use Params::Util qw(_ARRAYLIKE _CODELIKE);
use Scalar::Util qw(blessed);
use Sub::Exporter::Util ();
use Sub::Exporter -setup => [ flog => Sub::Exporter::Util::curry_method ];

=head1 SYNOPSIS
use String::Flogger qw(flog);
my @inputs = (
'simple!',
[ 'slightly %s complex', 'more' ],
[ 'and inline some data: %s', { look => 'data!' } ],
[ 'and we can defer evaluation of %s if we want', sub { 'stuff' } ],
sub { 'while avoiding sprintfiness, if needed' },
);
say flog($_) for @inputs;
The above will output:
simple!
slightly more complex
and inline some data: {{{ "look": "data!" }}}
and we can defer evaluation of %s if we want
while avoiding sprintfiness, if needed
=cut

sub _encrefs {
my ($self, $messages) = @_;
return map { ref $_ ? ('{{' . $self->_stringify_ref($_) . '}}') : $_ }
map { blessed($_) ? sprintf('obj(%s)', "$_") : $_ }
map { _CODELIKE($_) ? scalar $_->() : $_ }
@$messages;
}

my $JSON;
sub _stringify_ref {
my ($self, $ref) = @_;

require JSON;
$JSON ||= JSON->new
->ascii(1)
->canonical(1)
->allow_nonref(1)
->space_after(1)
->convert_blessed(1);

return $JSON->encode($ref)
}

sub flog {
my ($class, $input) = @_;

my $output;

if (_CODELIKE($input)) {
$input = $input->();
}

return $input unless ref $input;

if (_ARRAYLIKE($input)) {
my ($fmt, @data) = @$input;
return sprintf $fmt, $class->_encrefs(\@data);
}

return $class->_encrefs([ $input ]);
}

1;
31 changes: 31 additions & 0 deletions t/synopsis.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!perl
use strict;
use warnings;
use Test::More tests => 5;
use String::Flogger qw(flog);

is(
flog('simple!'),
'simple!',
);

is(
flog([ 'slightly %s complex', 'more' ]),
'slightly more complex',
);

is(
flog([ 'and inline some data: %s', { look => 'data!' } ]),
'and inline some data: {{{"look": "data!"}}}',
);

is(
flog([ 'and we can defer evaluation of %s if we want', sub { 'stuff' } ]),
'and we can defer evaluation of stuff if we want',
);

is(
flog(sub { 'while avoiding sprintfiness, if needed' }),
'while avoiding sprintfiness, if needed',
);

0 comments on commit 8260231

Please sign in to comment.