| 1 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.006; # Found with Perl::MinimumVersion | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Log::UDP::Client; | ||||||||||||||||||||||||||||
| 6 | 5 | $Log::UDP::Client::VERSION = '0.20.2'; | |||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | with 'Data::Serializable' => { -version => '0.40.0' }; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # ABSTRACT: A simple way to send structured log messages via UDP | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use IO::Socket::INET (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has "server_address" => ( | ||||||||||||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||||||||||||
| 18 | isa => 'Str', | ||||||||||||||||||||||||||||
| 19 | 4 | ▶ | default => sub { "127.0.0.1"; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 5 | ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | has "server_port" => ( | ||||||||||||||||||||||||||||
| 24 | is => 'rw', | ||||||||||||||||||||||||||||
| 25 | isa => 'Int', | ||||||||||||||||||||||||||||
| 26 | 3 | ▶ | default => sub { 9999; } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 5 | ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 5 | has "throws_exception" => ( | |||||||||||||||||||||||||||
| 31 | is => 'rw', | ||||||||||||||||||||||||||||
| 32 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 33 | default => 0, | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | has "socket" => ( | ||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||
| 39 | isa => 'IO::Socket::INET', | ||||||||||||||||||||||||||||
| 40 | lazy => 1, | ||||||||||||||||||||||||||||
| 41 | 4 | ▶ | default => sub { IO::Socket::INET->new( Proto => 'udp' ); } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 5 | ); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Perl::Critic bug: Subroutines::RequireArgUnpacking shouldn't be needed here | ||||||||||||||||||||||||||||
| 46 | ## no critic qw(Subroutines::ProhibitBuiltinHomonyms Subroutines::RequireArgUnpacking) | ||||||||||||||||||||||||||||
| 47 | 11003 | ▶ | sub send { | ||||||||||||||||||||||||||
|
Subroutine
send: called CC=7 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 11003 | my ($self, $message) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Make sure message was specified | ||||||||||||||||||||||||||||
| 51 | 11003 | ▶ | if ( @_ < 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | croak("Please specify message") if $self->throws_exception; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | return; # FAIL | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Use the specified serializer to encode the message in a binary format | ||||||||||||||||||||||||||||
| 57 | 11003 | my $serialized_message = $self->serialize( $message ); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Trap failure in serialization when not emitting exceptions | ||||||||||||||||||||||||||||
| 60 | 11003 | ▶ | if ( not $self->throws_exception and not defined($serialized_message) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->throws_exception or defined $serialized_message
MC/DC0%0/2
$self->throws_exception or defined $serialized_message
$self->throws_exception
defined $serialized_message
Truth table33%1/3
A or B
A= $self->throws_exceptionB= defined $serialized_message
| |||||||||||||||||||||||||||||
| 61 | 0 | return; # FAIL | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Send UDP message | ||||||||||||||||||||||||||||
| 65 | 11003 | my $length = CORE::send( | |||||||||||||||||||||||||||
| 66 | $self->socket, | ||||||||||||||||||||||||||||
| 67 | $serialized_message, | ||||||||||||||||||||||||||||
| 68 | 0, | ||||||||||||||||||||||||||||
| 69 | IO::Socket::INET::pack_sockaddr_in( | ||||||||||||||||||||||||||||
| 70 | $self->server_port, | ||||||||||||||||||||||||||||
| 71 | IO::Socket::INET::inet_aton( $self->server_address ) | ||||||||||||||||||||||||||||
| 72 | ) | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Check for transmission error | ||||||||||||||||||||||||||||
| 76 | 11003 | ▶ | if ( $length != length($serialized_message) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | my $error = "Couldn't send message: $!\n"; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | croak($error) if $self->throws_exception; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | carp($error); | |||||||||||||||||||||||||||
| 80 | 0 | return 0; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Everything OK | ||||||||||||||||||||||||||||
| 84 | 11003 | return 1; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 5 | 1; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||||||||||||