[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[SCM] Debian package checker branch, master, updated. 2.5.6-30-g6bca8f4



The following commit has been merged in the master branch:
commit 4774bc86c3d6847496fc16b8e06daec0bb4d728e
Author: Niels Thykier <niels@thykier.net>
Date:   Mon Mar 26 11:05:13 2012 +0200

    L::Data: Pass CURVALUE argument to CODE when loading data files
    
    Pass a new third argument to the CODE parameter when loading a data
    file.  The third argument (CURVALUE) is the current value associated
    with a given key (if any).  The CODE sub can modify CURVALUE to built
    more complex data structures from multiple lines.
    
    The CODE sub may now (only) return undef if CURVALUE is defined.  In
    this case, CURVALUE remains the value associated with the KEY.  This
    avoids needless creation of new values when the original value can be
    modified instead.
    
    With these changes it is possible to parse output/manual-references
    via L::Data instead of the ad-hoc parser currently used in
    L::Tag::Info.
    
    Signed-off-by: Niels Thykier <niels@thykier.net>

diff --git a/debian/changelog b/debian/changelog
index bdc0858..550957a 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -61,6 +61,8 @@ lintian (2.5.7) UNRELEASED; urgency=low
     + [NT] Lazily evaluate the data file.
   * lib/Lintian/Data.pm:
     + [NT] Lazily load data files.
+    + [NT] Allow pre-process sub to alter existing value for a key
+      by passing the previous value as third argument.
   * lib/Lintian/Profile.pm:
     + [NT] Normalize profile name and replace "parents" with
       "profile_list".  The latter also includes the current profile
diff --git a/lib/Lintian/Data.pm b/lib/Lintian/Data.pm
index 5e1c721..56baf5c 100644
--- a/lib/Lintian/Data.pm
+++ b/lib/Lintian/Data.pm
@@ -71,7 +71,15 @@ sub new {
                 my ($key, $val);
                 if (defined $separator) {
                     ($key, $val) = split(/$separator/, $_, 2);
-                    $val = $code->($key, $val) if $code;
+                    if ($code) {
+                        my $pval = $data{$type}{$key};
+                        $val = $code->($key, $val, $pval);
+                        next if ! defined $val && defined $pval;
+                        unless (defined $val) {
+                            next if defined $pval;
+                            croak "undefined value for $key (type: $type)";
+                        }
+                    }
                 } else {
                     ($key, $val) = ($_ => 1);
                 }
@@ -150,6 +158,26 @@ easily editable files.
 NB: By default Lintian::Data is lazy and defers loading of the data
 file until it is actually needed.
 
+=head2 Interface for the CODE argument
+
+This section describes the interface between for the CODE argument
+for the class method new.
+
+The sub will be called once for each key/pair with three arguments,
+KEY, VALUE and CURVALUE.  The first two are the key/value pair parsed
+from the data file and CURVALUE is current value assoicated with the
+key.  CURVALUE will be C<undef> the first time the sub is called with
+that KEY argument.
+
+The sub can then modify VALUE in some way and return the new value for
+that KEY.  If CURVALUE is not C<undef>, the sub may return C<undef> to
+indicate that the current value should still be used.  It is not
+permissible for the sub to return C<undef> if CURVALUE is C<undef>.
+
+Where Perl semantics allow it, the sub can modify CURVALUE and the
+changes will be reflected in the result.  As an example, if CURVALUE
+is a hashref, new keys can be inserted etc.
+
 =head1 CLASS METHODS
 
 =over 4
@@ -166,9 +194,7 @@ If SEPARATOR is given, it will be used as a regular expression for splitting
 the lines into key/value pairs.
 
 If CODE is also given, it is assumed to be a sub that will pre-process
-the key/value pairs.  The sub will be called once for each key/pair
-value with the key as the first and the value as the second argument.
-The value returned is will be used as the value for the key.
+the key/value pairs.  See the L</Interface for the CODE argument> above.
 
 A given file will only be loaded once.  If new() is called again with the
 same TYPE argument, the data previously loaded will be reused, avoiding
@@ -212,6 +238,11 @@ new() was called without a TYPE argument.
 The TYPE argument to new() did not correspond to a file in the F<data>
 directory of the Lintian root.
 
+=item undefined value for %s (type: %s)
+
+The CODE argument return undef for the KEY and no previous value for
+that KEY was available.
+
 =back
 
 =head1 FILES

-- 
Debian package checker


Reply to: