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