File Coverage

blib/lib/Net/AMQP/Value.pm
Criterion Covered Total %
statement 31 44 70.4
branch 2 12 16.6
condition n/a
subroutine 11 24 45.8
pod 0 1 0.0
total 44 81 54.3


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Net::AMQP::Value - A collection of classes for typing AMQP data
4              
5             =head1 SYNOPSIS
6              
7             use Net::AMQP::Value;
8              
9             # ... somewhere, in an AMQP table:
10              
11             Net::AMQP::Value::String->new("1") # not an integer
12             Net::AMQP::Value::Integer->new(" 1") # not a string
13             Net::AMQP::Value::Timestamp->new(1) # not an integer
14             Net::AMQP::Value::Boolean->new(1) # not an integer
15             Net::AMQP::Value::true # shorthand for ...Boolean->new(1)
16             Net::AMQP::Value::false # shorthand for ...Boolean->new(0)
17              
18             =head1 DESCRIPTION
19              
20             Generally in tables Net::AMQP tries to be smart, so e.g. a table value of
21             '1' or '-1' is transmitted as an integer. When this intelligence becomes a
22             problem, use these classes to type your data. For example, a table value of
23             Cnew(1)> will be transmitted as the string "1".
24              
25             These classes also overload the basics like "", 0+, and bool so if you use
26             them outside an AMQP table, they will probably Do The Right Thing.
27              
28             =head1 SEE ALSO
29              
30             L, L
31              
32             =cut
33              
34 5     5   49 use strict;
  5         7  
  5         156  
35 5     5   27 use Net::AMQP::Common ();
  5         8  
  5         603  
36              
37             package Net::AMQP::Value;
38 0     0   0 use overload '""' => sub { shift->[0] },
39 0 0   0   0 'cmp' => sub { ($_[0][0] cmp $_[1]) * ($_[2] ? -1 : 1) },
40 5 0   5   5399 '<=>' => sub { ($_[0][0] <=> $_[1]) * ($_[2] ? -1 : 1) };
  5     0   3051  
  5         60  
  0         0  
41 0     0 0 0 sub new { bless [ $_[1] ], $_[0] }
42              
43             package Net::AMQP::Value::String;
44 5     5   492 use base qw( Net::AMQP::Value );
  5         8  
  5         784  
45 0     0   0 sub field_packed { 'S' . Net::AMQP::Common::pack_long_string(shift->[0]) }
46              
47             package Net::AMQP::Value::Integer;
48 5     5   32 use base qw( Net::AMQP::Value );
  5         7  
  5         486  
49 5     5   24 use overload '0+' => sub { shift->[0] };
  5     0   8  
  5         30  
  0         0  
50 0 0   0   0 sub new { bless [ defined($_[1]) ? int($_[1]) : 0 ], $_[0] }
51 0     0   0 sub field_packed { 'I' . Net::AMQP::Common::pack_long_integer(shift->[0]) }
52              
53             package Net::AMQP::Value::Timestamp;
54 5     5   620 use base qw( Net::AMQP::Value::Integer ); # unsigned, but ok
  5         8  
  5         2686  
55 0     0   0 sub field_packed { 'T' . Net::AMQP::Common::pack_timestamp(shift->[0]) }
56              
57             package Net::AMQP::Value::Boolean;
58 5     5   25 use base qw( Net::AMQP::Value );
  5         8  
  5         774  
59 0     0   0 sub _num { shift->[0] }
60 0 0   0   0 sub _str { shift->[0] ? 'true' : 'false' };
61             use overload bool => \&_num,
62             '0+' => \&_num,
63             '""' => \&_str,
64 5 0   5   21 'cmp' => sub { (_str($_[0]) cmp $_[1]) * ($_[2] ? -1 : 1) };
  5     0   13  
  5         45  
  0         0  
65 10 100   10   536 sub new { bless [ $_[1] ? 1 : 0 ], $_[0] }
66 0     0     sub field_packed { 't' . Net::AMQP::Common::pack_boolean(shift->[0]) }
67              
68             package Net::AMQP::Value;
69             use constant {
70 5         20 false => Net::AMQP::Value::Boolean->new(0),
71             true => Net::AMQP::Value::Boolean->new(1),
72 5     5   787 };
  5         7  
73              
74             1;