example/TCPPack.pm

filter: Statementstmt 100% 37 / 37 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 97.9% 47 / 48 CC 1 SCAR 0.0
CC
1
Cov
97%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Devel/Trepan/CmdProcessor/Command/Load.pm ↑ Summary example/action-bug.pl »
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011 Rocky Bernstein <rocky@cpan.org>
3 # Subsidiary routines used to "pack" and "unpack" TCP messages.
4 2 ▶use strict; use warnings; no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package TCPPack;
7 2 ▶use POSIX qw(ceil log10);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 our (@ISA, @EXPORT);
10 1 @ISA = qw(Exporter);
11 1 @EXPORT = qw(TCP_MAX_PACKET LOG_MAX_MSG pack_msg unpack_msg);
12  
13 2 ▶use constant TCP_MAX_PACKET => 8192;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use constant LOG_MAX_MSG => ceil(log10(TCP_MAX_PACKET));
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub pack_msg($)
17 1 ▶{
Subroutine pack_msg: called CC=1 SCAR=0.0
18 1 my $msg = shift;
19 # A funny way of writing: '%04d'
20 1 my $fmt = sprintf '%%0%dd' , LOG_MAX_MSG;
21 1 return sprintf($fmt, length($msg)) . $msg;
22 }
23  
24 sub unpack_msg($)
25 1 ▶{
Subroutine unpack_msg: called CC=1 SCAR=0.0
26 1 my $buf = shift;
27 1 my $length = int(substr($buf, 0, LOG_MAX_MSG));
28 1 my $data = substr($buf, LOG_MAX_MSG, LOG_MAX_MSG + $length);
29 1 $buf = substr($buf, LOG_MAX_MSG + $length);
30 1 return ($buf, $data);
31 }
32  
33 # Demo
34 1 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 1 0 1
35 1 my $buf = "Hi there!";
36 1 my $msg;
37 1 ($buf, $msg) = unpack_msg(pack_msg($buf));
38 1 print "$msg\n";
39 }
40  
41 1;
« lib/Devel/Trepan/CmdProcessor/Command/Load.pm ↑ Summary example/action-bug.pl »