From: Matthias Bethke Date: Wed, 10 Sep 2014 16:37:26 +0000 (+0200) Subject: clean up Perl style a bit X-Git-Tag: collectd-5.5.0~130^2~2 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=bf66419c7726e85f31818d75aa521f4087719ca3;p=collectd.git clean up Perl style a bit - Remove some superfluous parenthesis clutter - Shorten a lot of single-line conditionals using postfix constructions - Merge variable declarations - Use $class/$self instead of $pkg/$obj as is customary - Remove quotes around literal hash keys --- diff --git a/bindings/perl/lib/Collectd/Unixsock.pm b/bindings/perl/lib/Collectd/Unixsock.pm index fdb2bbc0..f9981d98 100644 --- a/bindings/perl/lib/Collectd/Unixsock.pm +++ b/bindings/perl/lib/Collectd/Unixsock.pm @@ -33,7 +33,7 @@ collectd's unixsock plugin. =head1 SYNOPSIS - use Collectd::Unixsock (); + use Collectd::Unixsock; my $sock = Collectd::Unixsock->new ($path); @@ -56,23 +56,15 @@ programmers to interact with the daemon. use strict; use warnings; -#use constant { NOTIF_FAILURE => 1, NOTIF_WARNING => 2, NOTIF_OKAY => 4 }; - -use Carp (qw(cluck confess)); +use Carp qw(cluck confess carp croak); use IO::Socket::UNIX; use Scalar::Util qw( looks_like_number ); our $Debug = 0; -return (1); - sub _debug { - if (!$Debug) - { - return; - } - print @_; + print @_ if $Debug; } sub _create_socket @@ -89,88 +81,76 @@ sub _create_socket =head1 VALUE IDENTIFIERS -The values in the collectd are identified using an five-tuple (host, plugin, -plugin-instance, type, type-instance) where only plugin-instance and -type-instance may be NULL (or undefined). Many functions expect an -I<%identifier> hash that has at least the members B, B, and -B, possibly completed by B and B. +The values in the collectd are identified using a five-tuple (host, plugin, +plugin-instance, type, type-instance) where only plugin instance and type +instance may be undef. Many functions expect an I<%identifier> hash that has at +least the members B, B, and B, possibly completed by +B and B. Usually you can pass this hash as follows: - $obj->method (host => $host, plugin => $plugin, type => $type, %other_args); + $self->method (host => $host, plugin => $plugin, type => $type, %other_args); =cut sub _create_identifier { my $args = shift; - my $host; - my $plugin; - my $type; + my ($host, $plugin, $type); - if (!$args->{'host'} || !$args->{'plugin'} || !$args->{'type'}) + if (!$args->{host} || !$args->{plugin} || !$args->{type}) { cluck ("Need `host', `plugin' and `type'"); return; } - $host = $args->{'host'}; - $plugin = $args->{'plugin'}; - $plugin .= '-' . $args->{'plugin_instance'} if (defined ($args->{'plugin_instance'})); - $type = $args->{'type'}; - $type .= '-' . $args->{'type_instance'} if (defined ($args->{'type_instance'})); + $host = $args->{host}; + $plugin = $args->{plugin}; + $plugin .= '-' . $args->{plugin_instance} if defined $args->{plugin_instance}; + $type = $args->{type}; + $type .= '-' . $args->{type_instance} if defined $args->{type_instance}; - return ("$host/$plugin/$type"); + return "$host/$plugin/$type"; } # _create_identifier sub _parse_identifier { my $string = shift; - my $host; - my $plugin; - my $plugin_instance; - my $type; - my $type_instance; - my $ident; + my ($plugin_instance, $type_instance); - ($host, $plugin, $type) = split ('/', $string); + my ($host, $plugin, $type) = split /\//, $string; - ($plugin, $plugin_instance) = split ('-', $plugin, 2); - ($type, $type_instance) = split ('-', $type, 2); + ($plugin, $plugin_instance) = split /-/, $plugin, 2; + ($type, $type_instance) = split /-/, $type, 2; - $ident = + my $ident = { host => $host, plugin => $plugin, type => $type }; - $ident->{'plugin_instance'} = $plugin_instance if (defined ($plugin_instance)); - $ident->{'type_instance'} = $type_instance if (defined ($type_instance)); + $ident->{plugin_instance} = $plugin_instance if defined $plugin_instance; + $ident->{type_instance} = $type_instance if defined $type_instance; - return ($ident); + return $ident; } # _parse_identifier sub _escape_argument { - my $string = shift; + local $_ = shift; - if ($string =~ m/^\w+$/) - { - return ("$string"); - } - - $string =~ s#\\#\\\\#g; - $string =~ s#"#\\"#g; - $string = "\"$string\""; + return $_ if /^\w+$/; - return ($string); + s#\\#\\\\#g; + s#"#\\"#g; + return "\"$_\""; } =head1 PUBLIC METHODS =over 4 -=item I<$obj> = Collectd::Unixsock->B ([I<$path>]); +=item I<$self> = Collectd::Unixsock->B ([I<$path>]); Creates a new connection to the daemon. The optional I<$path> argument gives the path to the UNIX socket of the C and defaults to @@ -181,19 +161,18 @@ false on error. sub new { - my $pkg = shift; - my $path = @_ ? shift : '/var/run/collectd-unixsock'; + my $class = shift; + my $path = shift || '/var/run/collectd-unixsock'; my $sock = _create_socket ($path) or return; - my $obj = bless ( + return bless { path => $path, sock => $sock, error => 'No error' - }, $pkg); - return ($obj); + }, $class; } # new -=item I<$res> = I<$obj>-EB (I<%identifier>); +=item I<$res> = I<$self>-EB (I<%identifier>); Requests a value-list from the daemon. On success a hash-ref is returned with the name of each data-source as the key and the according value as, well, the @@ -203,15 +182,13 @@ value. On error false is returned. sub getval # {{{ { - my $obj = shift; + my $self = shift; my %args = @_; - my $status; - my $fh = $obj->{'sock'} or confess ('object has no filehandle'); - my $msg; - my $identifier; + my ($status, $msg, $identifier, $ret); + my $fh = $self->{sock} or confess ('object has no filehandle'); - my $ret = {}; + $ret = {}; $identifier = _create_identifier (\%args) or return; @@ -223,17 +200,17 @@ sub getval # {{{ chomp ($msg); _debug "<- $msg\n"; - ($status, $msg) = split (' ', $msg, 2); + ($status, $msg) = split / /, $msg, 2; if ($status <= 0) { - $obj->{'error'} = $msg; + $self->{error} = $msg; return; } - for (my $i = 0; $i < $status; $i++) + for (1 .. $status) { my $entry = <$fh>; - chomp ($entry); + chomp $entry; _debug "<- $entry\n"; if ($entry =~ m/^(\w+)=NaN$/) @@ -246,10 +223,10 @@ sub getval # {{{ } } - return ($ret); + return $ret; } # }}} sub getval -=item I<$res> = I<$obj>-EB (I<%identifier>); +=item I<$res> = I<$self>-EB (I<%identifier>); Requests a threshold from the daemon. On success a hash-ref is returned with the threshold data. On error false is returned. @@ -258,15 +235,13 @@ the threshold data. On error false is returned. sub getthreshold # {{{ { - my $obj = shift; + my $self = shift; my %args = @_; - my $status; - my $fh = $obj->{'sock'} or confess ('object has no filehandle'); - my $msg; - my $identifier; + my ($status, $msg, $identifier, $ret); + my $fh = $self->{sock} or confess ('object has no filehandle'); - my $ret = {}; + $ret = {}; $identifier = _create_identifier (\%args) or return; @@ -281,11 +256,11 @@ sub getthreshold # {{{ ($status, $msg) = split (' ', $msg, 2); if ($status <= 0) { - $obj->{'error'} = $msg; + $self->{error} = $msg; return; } - for (my $i = 0; $i < $status; $i++) + for (1 .. $status) { my $entry = <$fh>; chomp ($entry); @@ -296,17 +271,15 @@ sub getthreshold # {{{ my $key = $1; my $value = $2; - $key =~ s/^\s+//; - $key =~ s/\s+$//; - + $key =~ s/(?:^\s+|\s$)//; $ret->{$key} = $value; } } - return ($ret); + return $ret; } # }}} sub getthreshold -=item I<$obj>-EB (I<%identifier>, B