[libvirt] [PATCH perl v2] Remove use of Data::Dumper from example programs

Daniel P. Berrangé posted 1 patch 6 years, 2 months ago
Failed in applying to current master (apply log)
examples/dhcp-leases.pl |  7 +++++--
examples/dom-fsinfo.pl  |  8 +++-----
examples/dom-ifinfo.pl  | 14 +++++++++-----
examples/dom-stats.pl   |  9 +++------
examples/node-info.pl   | 26 +++++++++++++++++++-------
5 files changed, 39 insertions(+), 25 deletions(-)
[libvirt] [PATCH perl v2] Remove use of Data::Dumper from example programs
Posted by Daniel P. Berrangé 6 years, 2 months ago
Using Data::Dumper in examples does not help devs understand the data
structures that the Perl APIs are returning. Change to explicit field
accesses to illustrate it better

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 examples/dhcp-leases.pl |  7 +++++--
 examples/dom-fsinfo.pl  |  8 +++-----
 examples/dom-ifinfo.pl  | 14 +++++++++-----
 examples/dom-stats.pl   |  9 +++------
 examples/node-info.pl   | 26 +++++++++++++++++++-------
 5 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/examples/dhcp-leases.pl b/examples/dhcp-leases.pl
index 84a81ef..a2202d9 100644
--- a/examples/dhcp-leases.pl
+++ b/examples/dhcp-leases.pl
@@ -1,7 +1,6 @@
 #!/usr/bin/perl
 
 use Sys::Virt;
-use Data::Dumper;
 
 my $c = Sys::Virt->new(uri => "qemu:///system",
 		       readonly => 1);
@@ -9,5 +8,9 @@ my $c = Sys::Virt->new(uri => "qemu:///system",
 $n = $c->get_network_by_name("default");
 
 foreach my $lease ($n->get_dhcp_leases()) {
-    print Dumper($lease);
+    print "Interface ", $lease->{iface}, "\n";
+    print "   MAC: ", $lease->{mac}, "\n";
+    print "    IP: ", $lease->{ipaddr}, "\n";
+    print "  Host: ", $lease->{hostname}, "\n" if $lease->{hostname};
+    print "\n";
 }
diff --git a/examples/dom-fsinfo.pl b/examples/dom-fsinfo.pl
index 7763f78..46c64e9 100644
--- a/examples/dom-fsinfo.pl
+++ b/examples/dom-fsinfo.pl
@@ -1,6 +1,5 @@
 #!/usr/bin/perl
 
-
 use strict;
 use warnings;
 
@@ -15,7 +14,6 @@ my $dom = $c->get_domain_by_name(shift @ARGV);
 
 my @fs = $dom->get_fs_info();
 
-use Data::Dumper;
-
-print Dumper($fs[0]);
-print Dumper($fs[1]);
+foreach my $fs (@fs) {
+    printf "%s (%s) at %s\n", $fs->{name}, $fs->{fstype}, $fs->{mountpoint};
+}
diff --git a/examples/dom-ifinfo.pl b/examples/dom-ifinfo.pl
index e10579b..66eb157 100644
--- a/examples/dom-ifinfo.pl
+++ b/examples/dom-ifinfo.pl
@@ -1,6 +1,5 @@
 #!/usr/bin/perl
 
-
 use strict;
 use warnings;
 
@@ -13,9 +12,14 @@ my $c = Sys::Virt->new(uri => $uri);
 
 my $dom = $c->get_domain_by_name(shift @ARGV);
 
-my @fs = $dom->get_interface_addresses(
+my @nics = $dom->get_interface_addresses(
     Sys::Virt::Domain::INTERFACE_ADDRESSES_SRC_LEASE);
 
-use Data::Dumper;
-
-print Dumper(@fs);
+foreach my $nic (@nics) {
+    print "Interface ", $nic->{name}, "\n";
+    print "   MAC: ", $nic->{hwaddr}, "\n";
+    foreach my $addr (@{$nic->{addrs}}) {
+	print "    IP: ", $addr->{addr}, "\n";
+    }
+    print "\n";
+}
diff --git a/examples/dom-stats.pl b/examples/dom-stats.pl
index 13d8fb7..1da0089 100644
--- a/examples/dom-stats.pl
+++ b/examples/dom-stats.pl
@@ -20,10 +20,7 @@ my @stats = $c->get_all_domain_stats(Sys::Virt::Domain::STATS_STATE,
 				     \@doms,
 				     Sys::Virt::Domain::GET_ALL_STATS_ENFORCE_STATS);
 
-use Data::Dumper;
-
-print Dumper(\@stats);
-
-for (my $i = 0 ; $i <= $#stats ; $i++) {
-    print $stats[$i]->{'dom'}->get_name(), ": ", $stats[$i]->{'data'}->{'state.state'}, "\n";
+foreach my $stats (@stats) {
+    print "Guest ", $stats->{'dom'}->get_name(), "\n";
+    print "  State: ", $stats->{'data'}->{'state.state'}, "\n";
 }
diff --git a/examples/node-info.pl b/examples/node-info.pl
index 89bc9ba..9655ab4 100644
--- a/examples/node-info.pl
+++ b/examples/node-info.pl
@@ -13,13 +13,25 @@ my $info = $hv->get_node_info();
 
 my @models = $hv->get_cpu_model_names($info->{model});
 
-print join ("\n", sort{ lc $a cmp lc $b } @models), "\n";
-
-my @info = $hv->get_node_free_pages([2048], 0, 0);
-
-use Data::Dumper;
-print Dumper(\@info);
-
+print "Available CPU model names:\n";
+print join ("\n", map { "  " . $_ } sort{ lc $a cmp lc $b } @models), "\n";
+
+my @pagesizes = (
+    4, 2048, 1048576
+    );
+
+my @info = $hv->get_node_free_pages(\@pagesizes, 0, 0);
+
+print "Free pages per NUMA node:\n";
+foreach my $info (@info) {
+    print "  Node: ", $info->{cell}, "\n";
+    print "  Free: ";
+    for (my $i = 0; $i <= $#pagesizes; $i++) {
+	my $pagesize = $pagesizes[$i];
+	printf "%d @ %d KB, ", $info->{pages}->{$pagesize}, $pagesize;
+    }
+    print "\n";
+}
 
 my $xml = $hv->get_domain_capabilities(undef, "x86_64", undef, "kvm");
 print $xml;
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH perl v2] Remove use of Data::Dumper from example programs
Posted by John Ferlan 6 years, 2 months ago

On 02/06/2018 01:10 PM, Daniel P. Berrangé wrote:
> Using Data::Dumper in examples does not help devs understand the data
> structures that the Perl APIs are returning. Change to explicit field
> accesses to illustrate it better
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  examples/dhcp-leases.pl |  7 +++++--
>  examples/dom-fsinfo.pl  |  8 +++-----
>  examples/dom-ifinfo.pl  | 14 +++++++++-----
>  examples/dom-stats.pl   |  9 +++------
>  examples/node-info.pl   | 26 +++++++++++++++++++-------
>  5 files changed, 39 insertions(+), 25 deletions(-)
> 

Changes probably should mention this too..

Reviewed-by: John Ferlan <jferlan@redhat.com>

John

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list