File Coverage

blib/lib/Sereal/Encoder.pm
Criterion Covered Total %
statement 0 9 0.0
branch 0 14 0.0
condition n/a
subroutine 0 3 0.0
pod 1 2 50.0
total 1 28 3.5


line stmt bran cond sub pod time code
1             package Sereal::Encoder;
2             use 5.008;
3             use strict;
4             use warnings;
5             use Carp qw/croak/;
6             use XSLoader;
7              
8             our $VERSION= '5.003';
9             our $XS_VERSION= $VERSION; $VERSION= eval $VERSION;
10              
11             # Make sure to keep these constants in sync with the C code in srl_encoder.c.
12             # I know they could be exported from C using things like ExtUtils::Constant,
13             # but that's too much of a hassle for just three numbers.
14             #start-no-tidy
15             my ($compress_consts, $full_consts);
16             BEGIN {
17             $compress_consts= {
18             SRL_UNCOMPRESSED => 0,
19             SRL_SNAPPY => 1,
20             SRL_ZLIB => 2,
21             SRL_ZSTD => 3,
22             };
23             $full_consts=
24             #begin generated
25             {
26             'SRL_F_ALIASED_DEDUPE_STRINGS' => 4096,
27             'SRL_F_CANONICAL_REFS' => 32768,
28             'SRL_F_COMPRESS_SNAPPY' => 64,
29             'SRL_F_COMPRESS_SNAPPY_INCREMENTAL' => 128,
30             'SRL_F_COMPRESS_ZLIB' => 256,
31             'SRL_F_COMPRESS_ZSTD' => 262144,
32             'SRL_F_CROAK_ON_BLESS' => 4,
33             'SRL_F_DEDUPE_STRINGS' => 2048,
34             'SRL_F_ENABLE_FREEZE_SUPPORT' => 16384,
35             'SRL_F_ENCODER_COMPRESS_FLAGS_MASK' => 262592,
36             'SRL_F_NOWARN_UNKNOWN_OVERLOAD' => 512,
37             'SRL_F_NO_BLESS_OBJECTS' => 8192,
38             'SRL_F_REUSE_ENCODER' => 2,
39             'SRL_F_SHARED_HASHKEYS' => 1,
40             'SRL_F_SORT_KEYS' => 1024,
41             'SRL_F_SORT_KEYS_PERL' => 65536,
42             'SRL_F_SORT_KEYS_PERL_REV' => 131072,
43             'SRL_F_STRINGIFY_UNKNOWN' => 16,
44             'SRL_F_UNDEF_UNKNOWN' => 8,
45             'SRL_F_WARN_UNKNOWN' => 32,
46             '_FLAG_NAME' => [
47             'COMPRESS_SNAPPY',
48             'COMPRESS_SNAPPY_INCREMENTAL',
49             'COMPRESS_ZLIB',
50             'COMPRESS_ZSTD',
51             'SHARED_HASHKEYS',
52             'REUSE',
53             'CROAK_ON_BLESS',
54             'UNDEF_UNKNOWN',
55             'STRINGIFY_UNKNOWN',
56             'WARN_UNKNOWN',
57             'COMPRESS_SNAPPY',
58             'COMPRESS_SNAPPY_INCREMENTAL',
59             'COMPRESS_ZLIB',
60             'NOWARN_UNKNOWN_OVERLOAD',
61             'SORT_KEYS',
62             'DEDUPE_STRINGS',
63             'ALIASED_DEDUPE_STRINGS',
64             'NO_BLESS_OBJECTS',
65             'ENABLE_FREEZE_SUPPORT',
66             'CANONICAL_REFS',
67             'SORT_KEYS_PERL',
68             'SORT_KEYS_PERL_REV',
69             'COMPRESS_ZSTD'
70             ]
71             };
72             #end generated
73             }
74             use constant $compress_consts;
75             use constant $full_consts;
76             #end-no-tidy
77              
78             use Exporter 'import';
79             our @EXPORT_OK= (qw(
80             encode_sereal
81             encode_sereal_with_header_data
82             sereal_encode_with_object
83             ), sort { $a cmp $b }
84             (
85             keys(%$compress_consts),
86             keys(%$full_consts)
87             ));
88             our %EXPORT_TAGS= (
89             all => \@EXPORT_OK,
90             compress_const => [ sort keys %$compress_consts ],
91             full_const => [ sort(keys(%$compress_consts),keys(%$full_consts)) ],
92             );
93              
94             # export by default if run from command line
95             our @EXPORT= ( ( caller() )[1] eq '-e' ? @EXPORT_OK : () );
96              
97 0     0     sub CLONE_SKIP { 1 }
98              
99             XSLoader::load( 'Sereal::Encoder', $XS_VERSION );
100              
101             sub encode_to_file {
102 0     0 1   my ( $self, $file, $struct, $append )= @_;
103 0 0         $self= $self->new() unless ref $self;
104 0 0         my $mode= $append ? ">>" : ">";
105 0 0         open my $fh, $mode, $file
    0          
106             or die "Failed to open '$file' for " . ( $append ? "append" : "write" ) . ": $!";
107 0 0         print $fh $self->encode($struct)
108             or die "Failed to print to '$file': $!";
109 0 0         close $fh
110             or die "Failed to close '$file': $!";
111             }
112              
113             my $flags= sub {
114             my ( $int, $ary )= @_;
115             return map { ( $ary->[$_] and $int & ( 1 << $_ ) ) ? $ary->[$_] : () } ( 0 .. $#$ary );
116             };
117              
118             sub flag_names {
119 0     0 0   my ( $self, $val )= @_;
120 0 0         return $flags->( defined $val ? $val : $self->flags, _FLAG_NAME );
121             }
122              
123             1;
124              
125             __END__