File Coverage

blib/lib/STIX/Common/Hex.pm
Criterion Covered Total %
statement 24 25 96.0
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 37 39 94.8


line stmt bran cond sub pod time code
1             package STIX::Common::Hex;
2              
3 24     24   472 use 5.010001;
  24         115  
4 24     24   177 use strict;
  24         86  
  24         810  
5 24     24   147 use warnings;
  24         88  
  24         1330  
6 24     24   3527 use utf8;
  24         75  
  24         167  
7              
8 24     24   1594 use overload '""' => \&to_string, fallback => 1;
  24         50  
  24         322  
9              
10 24     24   2623 use Carp;
  24         52  
  24         2437  
11 24     24   178 use Moo;
  24         51  
  24         245  
12              
13             around BUILDARGS => sub {
14              
15             my ($orig, $class, @args) = @_;
16              
17             return {value => $args[0]} if @args == 1;
18             return $class->$orig(@args);
19              
20             };
21              
22             my $HEX_REGEXP = qr{^([a-fA-F0-9]{2})+$};
23              
24             has value => (
25             is => 'rw',
26             isa => sub { Carp::croak 'MUST be hex-encoded string' unless $_[0] =~ /$HEX_REGEXP/ },
27             coerce => sub { _parse($_[0]) }
28             );
29              
30             sub _parse {
31              
32 4     4   12 my $bin = shift;
33              
34 4 50       146 return $bin if $bin =~ /$HEX_REGEXP/;
35 0         0 return unpack('H*', $bin);
36              
37             }
38              
39 81     81 1 14657 sub to_string { shift->value }
40 79     79 1 2265 sub TO_JSON { shift->value }
41              
42              
43             1;
44              
45             =encoding utf-8
46              
47             =head1 NAME
48              
49             STIX::Common::Hex - Hex type
50              
51             =head1 SYNOPSIS
52              
53             use STIX::Common::Hex;
54              
55             my $hex_object = STIX::Common::Hex->new(value => $bin);
56              
57             say $hex_object; # hex-encoded string
58              
59              
60             =head1 DESCRIPTION
61              
62             The hex data type encodes an array of octets (8-bit bytes) as hexadecimal. The
63             string MUST consist of an even number of hexadecimal characters, which are the
64             digits '0' through '9' and the lower-case letters 'a' through 'f'. In order to
65             allow pattern matching on custom objects, for all properties that use the hex
66             type, the property name MUST end with '_hex'.
67              
68             =head2 PROPERTIES
69              
70             =over
71              
72             =item value
73              
74             =back
75              
76             =head2 HELPERS
77              
78             =over
79              
80             =item $hex->TO_JSON
81              
82             Encode the object in JSON.
83              
84             =item $hex->to_string
85              
86             Encode the object in JSON.
87              
88             =back
89              
90              
91             =head1 SUPPORT
92              
93             =head2 Bugs / Feature Requests
94              
95             Please report any bugs or feature requests through the issue tracker
96             at L.
97             You will be notified automatically of any progress on your issue.
98              
99             =head2 Source Code
100              
101             This is open source software. The code repository is available for
102             public review and contribution under the terms of the license.
103              
104             L
105              
106             git clone https://github.com/giterlizzi/perl-STIX.git
107              
108              
109             =head1 AUTHOR
110              
111             =over 4
112              
113             =item * Giuseppe Di Terlizzi
114              
115             =back
116              
117              
118             =head1 LICENSE AND COPYRIGHT
119              
120             This software is copyright (c) 2024 by Giuseppe Di Terlizzi.
121              
122             This is free software; you can redistribute it and/or modify it under
123             the same terms as the Perl 5 programming language system itself.
124              
125             =cut