diff options
author | Yorhel <git@yorhel.nl> | 2015-10-03 10:15:53 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2015-10-03 10:15:53 +0200 |
commit | b75eff0f2cce2050c98ec946aac28b390020fdc5 (patch) | |
tree | 2e34fdd690cb06b5f5cb12dbecaa3919e0d16ec6 /lib/VNDB/Func.pm | |
parent | 30caa944332fbbb9f6f5a4805f5beeee2b8a506c (diff) |
Handle JSON data natively when processing form data
No more need for extra json_encode/json_decode calls, and the
form_compare() function is more lenient w.r.t. integer/string
comparison.
This is the improvement I described in commit
ed86cfd12b0bed7352e2be525b8e63cb4d6d5448
Diffstat (limited to 'lib/VNDB/Func.pm')
-rw-r--r-- | lib/VNDB/Func.pm | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/VNDB/Func.pm b/lib/VNDB/Func.pm index 02489980..b4848796 100644 --- a/lib/VNDB/Func.pm +++ b/lib/VNDB/Func.pm @@ -13,6 +13,7 @@ our @EXPORT = (@VNDBUtil::EXPORT, qw| childtags charspoil imgpath imgurl fmtvote json_encode json_decode script_json mtvoiced mtani mtvnlen mtrlstat mtvnlstat mtbloodt + form_compare |); @@ -237,5 +238,22 @@ sub mtrlstat { !$_[0] ? mt '_unknown' : mt '_rlist_status_'.$_[0]; } sub mtvnlstat{ !$_[0] ? mt '_unknown' : mt '_vnlist_status_'.$_[0]; } sub mtbloodt { $_[0] eq 'unknown' ? mt '_unknown' : mt '_bloodt_'.$_[0]; } + +# Compare the keys in %$old with the keys in %$new. Returns 1 if a difference was found, 0 otherwise. +sub form_compare { + my($old, $new) = @_; + for my $k (keys %$old) { + my($o, $n) = ($old->{$k}, $new->{$k}); + return 1 if !defined $n || ref $o ne ref $n; + if(!ref $o) { + return 1 if $o ne $n; + } else { # 'json' template + return 1 if @$o != @$n; + return 1 if grep form_compare($o->[$_], $n->[$_]), 0..$#$o; + } + } + return 0; +} + 1; |