File Coverage

blib/lib/Test/Smoke/Util/Serialise.pm
Criterion Covered Total %
statement 22 22 100.0
branch 8 8 100.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 35 35 100.0


line stmt bran cond sub pod time code
1             package Test::Smoke::Util::Serialise;
2 9     9   111515 use warnings;
  9         34  
  9         293  
3 9     9   56 use strict;
  9         19  
  9         186  
4              
5 9     9   53 use Exporter 'import';
  9         32  
  9         2745  
6             our @EXPORT_OK = qw( serialise );
7              
8             =head1 NAME
9              
10             Test::Smoke::Util::Serialise - Serialise (stringify) values, a bit like
11             L.
12              
13             =head1 SYNOPSIS
14              
15             use Test::Smoke::Util::Serialise 'serialise';
16              
17             my $value = [ qw( one two three ) ];
18             printf "Looks like: '%s'\n", serialise($value);
19             # Looks like: '[one, two, three]'\n
20              
21             =head1 DESCRIPTION
22              
23             Mostly looks like L, with C<$Indent = 0>, C<$Sortkeys = 1>
24             and C<$Terse = 1>.
25              
26             =head2 serialise($to_serialise)
27              
28             Make a string representation of the argument passed.
29              
30             Arrays are represented with enclosing square brackets
31              
32             Hashes are represented with enclosing curly braces, where all te Key-Value-pairs
33             have enclosing parenthesis with a C<< => >> (fat comma) in-between.
34              
35             {(one => two), (three => [four, five]), (six => {(seven => eight)}, (nine => \ten))}
36              
37             =head3 Arguments
38              
39             Positional:
40              
41             =over
42              
43             =item 1. B<$to_serialise>
44              
45             =back
46              
47             =head3 Responses
48              
49             A string representation of the value passed.
50              
51             =cut
52              
53             sub serialise {
54 13     13 1 1798 my ($to_serialise) = @_;
55              
56 13         18 GIVEN {
57 13         25 local $_ = ref($to_serialise);
58              
59 13 100       38 m{^ SCALAR $}x and do {
60 1         8 return "\\" . serialise($$to_serialise);
61             };
62 12 100       28 m{^ ARRAY $}x and do {
63             return sprintf(
64             "[%s]",
65 1         3 join(", ", map { serialise($_) } @$to_serialise)
  2         6  
66             );
67             };
68 11 100       29 m{^ HASH $}x and do {
69             return sprintf(
70             "{%s}",
71             join(
72             ", ",
73             map {
74 1         7 sprintf("(%s => %s)", $_, serialise($to_serialise->{$_}))
  3         7  
75             } sort keys %$to_serialise
76             )
77             );
78             };
79             # default, trust stringify
80 10         13 do {
81 10 100       99 return defined($to_serialise) ? "$to_serialise" : undef;
82             };
83             }
84             }
85              
86             1;
87              
88             =head1 COPYRIGHT
89              
90             (c) 2020, Abe Timmerman All rights reserved.
91              
92             With contributions from Jarkko Hietaniemi, Merijn Brand, Campo
93             Weijerman, Alan Burlison, Allen Smith, Alain Barbet, Dominic Dunlop,
94             Rich Rauenzahn, David Cantrell.
95              
96             This library is free software; you can redistribute it and/or modify
97             it under the same terms as Perl itself.
98              
99             See:
100              
101             =over 4
102              
103             =item * L
104              
105             =item * L
106              
107             =back
108              
109             This program is distributed in the hope that it will be useful,
110             but WITHOUT ANY WARRANTY; without even the implied warranty of
111             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
112              
113             =cut