From da13bf31d5ff7c72eeeb37f5e1ef4fab3b5e2319 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Wed, 5 Apr 2017 23:30:41 +0200 Subject: [PATCH] add |more support to list+show commands --- Inspect.pm | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/Inspect.pm b/Inspect.pm index c0c6571..1ed73cf 100644 --- a/Inspect.pm +++ b/Inspect.pm @@ -21,7 +21,7 @@ no strict 'refs'; use Data::Dumper; -$Data::Interactive::Inspect::VERSION = 0.06; +$Data::Interactive::Inspect::VERSION = 0.07; use vars qw(@ISA); @@ -330,6 +330,17 @@ sub _failidx { return $self->_fail(" must be a number, since we're inside an array\n"); } +sub _more { + my ($self, $out) = @_; + if (open LESS, "|$self->{more}") { + print LESS $out; + close LESS; + } + else { + print $out; + } +} + sub quit { my $self = shift; $self->{quit} = 1; @@ -526,13 +537,7 @@ sub dump { return $out; } else { - if (open LESS, "|$self->{more}") { - print LESS $out; - close LESS; - } - else { - print $out; - } + $self->_more($out); } return 1; @@ -586,8 +591,8 @@ sub list { $self->show; } else { - print join "\n", sort keys %{$self->{db}}; - print "\n"; + my $out = join("\n", sort keys %{$self->{db}}) . "\n"; + $self->_more($out); } return 1; @@ -595,24 +600,27 @@ sub list { sub show { my ($self, $indent) = @_; + my $out; if (ref($self->{db}) =~ /array/i) { my $pos = 0; foreach my $item (@{$self->{db}}) { - print "$pos:\n"; + $out .= "$pos:\n"; if (ref($item)) { - $self->_showhash($item, " "); + $out .= $self->_showhash($item, " "); } else { - print " $item\n"; + $out .= " $item\n"; } $pos++; } } else { - $self->_showhash($self->{db}); + $out = $self->_showhash($self->{db}); } + $self->_more($out); + return 1; } @@ -623,18 +631,21 @@ sub _showhash { $indent = ''; } + my $out; foreach my $key (sort keys %{$db}) { - printf "%s%-30s", $indent, $key; + $out .= sprintf "%s%-30s", $indent, $key; if (ref($db->{$key}) =~ /hash/i) { - print "{ .. }\n"; + $out .= "{ .. }\n"; } elsif (ref($db->{$key}) =~ /array/i) { - print "[ .. ]\n"; + $out .= "[ .. ]\n"; } else { - print "\"$db->{$key}\"\n"; + $out .= "\"$db->{$key}\"\n"; } } + + return $out; } sub enter { @@ -1148,6 +1159,6 @@ and/or modify it under the same terms as Perl itself. =head1 VERSION -This is the manual page for L Version 0.06. +This is the manual page for L Version 0.07. =cut