fixed error handling

This commit is contained in:
git@daemon.de
2015-02-06 18:09:15 +01:00
parent 4766fb13b8
commit 26ee4e41fe
2 changed files with 68 additions and 41 deletions

View File

@@ -1,2 +1,8 @@
0.02
interactive command errors now lead to abort of
inspect() if reading from STDIN.
fixed "get struct"
0.01 0.01
initial commit initial commit

View File

@@ -19,7 +19,7 @@ use strict;
use warnings; use warnings;
no strict 'refs'; no strict 'refs';
$Data::Interactive::Inspect::VERSION = 0.01; $Data::Interactive::Inspect::VERSION = 0.02;
use vars qw(@ISA); use vars qw(@ISA);
@@ -133,10 +133,10 @@ sub inspect {
my ($self, $__cmds) = @_; my ($self, $__cmds) = @_;
if ($__cmds) { if ($__cmds) {
# unit tests # unit tests und scripts
$self->{silent} = 1; $self->{silent} = 1;
foreach (split /\n/, $__cmds) { foreach (split /\n/, $__cmds) {
if (! $self->process($_) ) { if (! $self->process($_, 1) ) {
last; last;
} }
} }
@@ -168,7 +168,7 @@ sub inspect {
} }
else { else {
while (<STDIN>) { while (<STDIN>) {
if (! $self->process($_) ) { if (! $self->process($_, 1) ) {
last; last;
} }
} }
@@ -249,10 +249,11 @@ sub complete {
} }
sub process { sub process {
my ($self, $line) = @_; my ($self, $line, $failonerr) = @_;
return 1 if(!defined $line); return 1 if(!defined $line);
my $r;
my ($cmd, @args) = split /\s\s*/, $line; my ($cmd, @args) = split /\s\s*/, $line;
return 1 if (!defined $cmd); return 1 if (!defined $cmd);
@@ -271,14 +272,17 @@ sub process {
if (! grep {$cmd eq $_} @{$self->{commandargs}}) { if (! grep {$cmd eq $_} @{$self->{commandargs}}) {
@args = (); @args = ();
} }
$self->$func(@args); $r = $self->$func(@args);
return 0 if($failonerr && !$r); # fail if not interactive
} }
else { else {
if (exists $self->{db}->{$cmd}) { if (exists $self->{db}->{$cmd}) {
$self->enter($cmd); $r = $self->enter($cmd);
return 0 if($failonerr && !$r); # fail if not interactive
} }
else { else {
print STDERR "no such command: $cmd\n"; print STDERR "no such command: $cmd\n";
return 0 if $failonerr;
} }
} }
} }
@@ -290,11 +294,16 @@ sub process {
# command implementations # command implementations
sub __interactive__ {}; sub __interactive__ {};
sub fail {
my ($self, $msg) = @_;
print STDERR $msg;
return 0;
}
sub set { sub set {
my($self, $key, @value) = @_; my($self, $key, @value) = @_;
if (!$key) { if (!$key) {
print STDERR "<key> parameter missing\n"; return $self->fail("<key> parameter missing\n");
return;
} }
my $var; my $var;
@@ -305,25 +314,24 @@ sub set {
eval $code; eval $code;
} }
if ($@) { if ($@) {
print STDERR "failed to insert: $@\n"; return $self->fail("failed to insert: $@\n");
} }
else { else {
$self->{db}->{$key} = $var; $self->{db}->{$key} = $var;
$self->done; $self->done;
} }
return 1;
} }
sub append { sub append {
my($self, $key, @value) = @_; my($self, $key, @value) = @_;
if (!$key) { if (!$key) {
print STDERR "<key> parameter missing\n"; return $self->fail("<key> parameter missing\n");
return;
} }
if (exists $self->{db}->{$key}) { if (exists $self->{db}->{$key}) {
if (ref($self->{db}->{$key}) !~ /array/i) { if (ref($self->{db}->{$key}) !~ /array/i) {
print STDERR "\"$key\" already exists and is not an array\n"; return $self->fail("\"$key\" already exists and is not an array\n");
return;
} }
} }
@@ -331,19 +339,20 @@ sub append {
my $code = "\$var = @value;"; my $code = "\$var = @value;";
eval $code; eval $code;
if ($@) { if ($@) {
print STDERR "failed to insert: $@\n"; return $self->fail("failed to insert: $@\n");
} }
else { else {
push @{$self->{db}->{$key}}, $var; push @{$self->{db}->{$key}}, $var;
$self->done; $self->done;
} }
return 1;
} }
sub drop { sub drop {
my($self, $key) = @_; my($self, $key) = @_;
if (!$key) { if (!$key) {
print STDERR "<key> parameter missing\n"; return $self->fail("<key> parameter missing\n");
return;
} }
if (exists $self->{db}->{$key}) { if (exists $self->{db}->{$key}) {
@@ -351,49 +360,50 @@ sub drop {
$self->done; $self->done;
} }
else { else {
print STDERR "no such key: \"$key\"\n"; return $self->fail("no such key: \"$key\"\n");
} }
return 1;
} }
sub mypop { sub mypop {
my($self, $key) = @_; my($self, $key) = @_;
if (!$key) { if (!$key) {
print STDERR "<key> parameter missing\n"; return $self->fail("<key> parameter missing\n");
return;
} }
if (exists $self->{db}->{$key}) { if (exists $self->{db}->{$key}) {
if (ref($self->{db}->{$key}) !~ /array/i) { if (ref($self->{db}->{$key}) !~ /array/i) {
print STDERR "\"$key\" is not an array\n"; return $self->fail("\"$key\" is not an array\n");
return;
} }
} }
my $ignore = pop @{$self->{db}->{$key}}; my $ignore = pop @{$self->{db}->{$key}};
$self->done; $self->done;
return 1;
} }
sub myshift { sub myshift {
my($self, $key) = @_; my($self, $key) = @_;
if (!$key) { if (!$key) {
print STDERR "<key> parameter missing\n"; return $self->fail("<key> parameter missing\n");
return;
} }
if (exists $self->{db}->{$key}) { if (exists $self->{db}->{$key}) {
if (ref($self->{db}->{$key}) !~ /array/i) { if (ref($self->{db}->{$key}) !~ /array/i) {
print STDERR "\"$key\" is not an array\n"; return $self->fail("\"$key\" is not an array\n");
return;
} }
} }
my $ignore = shift @{$self->{db}->{$key}}; my $ignore = shift @{$self->{db}->{$key}};
$self->done; $self->done;
return 1;
} }
sub get { sub get {
my($self, $key, $search) = @_; my($self, $key, $search) = @_;
if (!$key) { if (!$key) {
print STDERR "<key> parameter missing\n"; return $self->fail("<key> parameter missing\n");
return;
} }
my $out; my $out;
@@ -413,21 +423,22 @@ sub get {
push @K, $key; push @K, $key;
} }
else { else {
print STDERR "no such key: \"$key\"\n"; return $self->fail("no such key: \"$key\"\n");
return;
} }
} }
foreach my $key (@K) { foreach my $key (@K) {
if (ref($self->{db}->{$key}) =~ /hash/i || ref($self->{db}->{$key}) =~ /array/i) { if (ref($self->{db}->{$key}) =~ /hash/i || ref($self->{db}->{$key}) =~ /array/i) {
# FIXME: something nicer # FIXME: something nicer
$out .= "$key =>\n" . &dump($self->{db}->{$key}, 1) $out .= "$key =>\n" . $self->dump($self->{db}->{$key}, 1)
} }
else { else {
$out .= "$key => \"$self->{db}->{$key}\"\n"; $out .= "$key => \"$self->{db}->{$key}\"\n";
} }
} }
print $out; print $out;
return 1;
} }
sub dump { sub dump {
@@ -452,13 +463,14 @@ sub dump {
print $out; print $out;
} }
} }
return 1;
} }
sub edit { sub edit {
my ($self, $key) = @_; my ($self, $key) = @_;
if (!$key) { if (!$key) {
print STDERR "<key> parameter missing\n"; return $self->fail("<key> parameter missing\n");
return;
} }
if (exists $self->{db}->{$key}) { if (exists $self->{db}->{$key}) {
@@ -480,7 +492,7 @@ sub edit {
$perl = YAML::Load($newdata); $perl = YAML::Load($newdata);
}; };
if ($@) { if ($@) {
print STDERR "$@\n"; return $self->fail("$@\n");
} }
else { else {
$self->{db}->{$key} = $perl; $self->{db}->{$key} = $perl;
@@ -490,14 +502,18 @@ sub edit {
unlink($filename); unlink($filename);
} }
else { else {
print STDERR "no such key: \"$key\"\n"; return $self->fail("no such key: \"$key\"\n");
} }
return 1;
} }
sub list { sub list {
my $self = shift; my $self = shift;
print join "\n", sort keys %{$self->{db}}; print join "\n", sort keys %{$self->{db}};
print "\n"; print "\n";
return 1;
} }
sub show { sub show {
@@ -514,13 +530,14 @@ sub show {
print "\"$self->{db}->{$key}\"\n"; print "\"$self->{db}->{$key}\"\n";
} }
} }
return 1;
} }
sub enter { sub enter {
my ($self, $key) = @_; my ($self, $key) = @_;
if (!$key) { if (!$key) {
print STDERR "<key> parameter missing\n"; return $self->fail("<key> parameter missing\n");
return;
} }
if ($key eq '..') { if ($key eq '..') {
@@ -536,13 +553,15 @@ sub enter {
print "=> $key\n"; print "=> $key\n";
} }
else { else {
print STDERR "not a hash: \"$key\"\n"; return $self->fail("not a hash: \"$key\"\n");
} }
} }
else { else {
print STDERR "unknown command \"$key\"\n"; return $self->fail("unknown command \"$key\"\n");
} }
} }
return 1;
} }
sub up { sub up {
@@ -553,8 +572,10 @@ sub up {
print "<=\n"; print "<=\n";
} }
else { else {
print STDERR "already on top level\n"; return $self->fail("already on top level\n");
} }
return 1;
} }
sub done { sub done {
@@ -925,6 +946,6 @@ and/or modify it under the same terms as Perl itself.
=head1 VERSION =head1 VERSION
This is the manual page for L<Data::Interactive::Inspect> Version 0.01. This is the manual page for L<Data::Interactive::Inspect> Version 0.02.
=cut =cut