File Coverage

blib/lib/String/Binary/Interpolation.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 17 17 100.0


line stmt bran cond sub pod time code
1             package String::Binary::Interpolation;
2              
3 2     2   507200 use strict;
  2         6  
  2         82  
4 2     2   11 use warnings;
  2         4  
  2         221  
5              
6             # Yuck, semver. I give in, the stupid cult that doesn't understand
7             # what the *number* bit of *version number* means has won.
8             our $VERSION = '1.0.1';
9              
10             sub import {
11 1     1   18 my $victim = (caller(0))[0];
12 1         5 foreach my $byte (0 .. 255) {
13 2     2   15 no strict 'refs';
  2         5  
  2         216  
14 256         560 *{sprintf('%s::b%08b', $victim, $byte)} = \chr($byte);
  256         3307  
15             }
16             }
17              
18             1;
19              
20             =head1 NAME
21              
22             String::Binary::Interpolation - make it easier to interpolate binary bytes into a string
23              
24             =head1 SYNOPSIS
25              
26             Where you would previously have had to write something like this ...
27              
28             my $binary = "ABC@{[chr(0b01000100)]}E"
29              
30             or ...
31              
32             my $binary = 'ABC'.chr(0b01000100).'E';
33              
34             to interpolate some random byte into a string you can now do this ...
35              
36             use String::Binary::Interpolation;
37              
38             my $binary = "ABC${b01000100}E";
39              
40             which I think you'll agree is much easier to read.
41              
42             =head1 BUT WHY!?!?!?
43              
44             Bit-fields, dear reader. If you are writing data to a binary file, and that
45             file contains bytes (or even longer words) which are bit-fields, it is easier
46             to have the bits of the bit-field right there in your string instead of having
47             to glue the string together from various parts, and it's far easier to read
48             than the frankly evil hack of embedding an array-ref.
49              
50             =head1 OK, SO WHAT DOES IT DO?
51              
52             When you C the module all it does is create a bunch of varliables in
53             your namespace. They are named from C<$b00000000> to C<$b11111111> and their
54             values are the corresponding characters. NB that when writing files containing
55             characters with the high-bit set you need to be careful that you read and write
56             B and not some unicode jibber-jabber.
57              
58             =head1 SOURCE CODE REPOSITORY
59              
60             L
61              
62             =head1 BUGS
63              
64             Bug reports and requests for extra features should be made on Github.
65              
66             =head1 AUTHOR
67              
68             David Cantrell Edavid@cantrell.org.ukE
69              
70             =head1 COPYRIGHT and LICENCE
71              
72             Copyright (c) 2020 David Cantrell. This program is free software; you can
73             redistribute it and/or modify it under the terms of the Artistic Licence
74             or the GNU General Public Licence version 2, the full text of which is
75             included in this distribution, in the files ARTISTIC.txt and GPL2.txt.
76              
77             =head1 SEE ALSO
78              
79             L's section on quote and quote-like operators
80