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