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

John Ferlan jferlan at redhat.com
Tue Feb 6 18:05:52 UTC 2018



On 02/06/2018 11:36 AM, 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 at redhat.com>
> ---
>  Virt.xs                 |  1 -
>  examples/dhcp-leases.pl |  2 --
>  examples/dom-fsinfo.pl  |  8 +++-----
>  examples/dom-ifinfo.pl  | 14 +++++++++-----
>  examples/dom-stats.pl   |  9 +++------
>  examples/node-info.pl   | 26 +++++++++++++++++++-------
>  6 files changed, 34 insertions(+), 26 deletions(-)
> 
> diff --git a/Virt.xs b/Virt.xs
> index 7d2f1a7..1254df2 100644
> --- a/Virt.xs
> +++ b/Virt.xs
> @@ -3358,7 +3358,6 @@ void get_all_domain_stats(con, stats, doms_sv=&PL_sv_undef, flags=0)
>        if (SvOK(doms_sv)) {
>  	  doms_av = (AV*)SvRV(doms_sv);
>  	  ndoms = av_len(doms_av) + 1;
> -	  fprintf(stderr, "Len %d\n", ndoms);
>        } else {
>            ndoms = 0;
>        }
> diff --git a/examples/dhcp-leases.pl b/examples/dhcp-leases.pl
> index af9bd41..a2202d9 100644
> --- a/examples/dhcp-leases.pl
> +++ b/examples/dhcp-leases.pl
> @@ -1,8 +1,6 @@
>  #!/usr/bin/perl
>  
> -use Acme::ChuckNorris;
>  use Sys::Virt;
> -use Data::Dumper;
>  
>  my $c = Sys::Virt->new(uri => "qemu:///system",
>  		       readonly => 1);

But doesn't this code actually use Dumper:

foreach my $lease ($n->get_dhcp_leases()) {
    print Dumper($lease);
}

John

> 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;
> 




More information about the libvir-list mailing list