View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Robert Krimen > Hash-Dirty-0.023 > Hash::Dirty



Annotate this POD

View/Report Bugs
Module Version: 0.023   Source  


Hash::Dirty - Keep track of whether a hash is dirty or not


Version 0.023


    use Hash::Dirty;

    my %hash;
    tie %hash, qw/Hash::Dirty/, { a => 1 };

    (tied %hash)->is_dirty; # Nope, not dirty yet.

    $hash{a} = 1;
    (tied %hash)->is_dirty; # Still not dirty yet.

    $hash{b} = 2;
    (tied %hash)->is_dirty; # Yes, now it's dirty

    (tied %hash)->dirty_keys; # ( b )

    $hash{a} = "hello";
    (tied %hash)->dirty_keys; # ( a, b )

    (tied %hash)->dirty_values; # ( "hello", 2 )

    (tied %hash)->dirty } # { a => 1, b => 1 }

    (tied %hash)->reset;
    (tied %hash)->is_dirty; # Nope, not dirty anymore.

    $hash{c} = 3;
    (tied %hash)->is_dirty; # Yes, dirty again.

    # %hash is { a => "hello", b => 2, c => 3 }
    (tied %hash)->dirty_slice } # { c => 3 }

    # Alternately:

    use Hash::Dirty;

    my $hash = Hash::Dirty::hash;

    # Also:
    my ($object, $hash) = Hash::Dirty->new;
    $hash->{a} = 1; # Etc., etc.


Hash::Dirty will keep track of the dirty keys in a hash, letting you which values changed.

Currently, Hash::Dirty will only inspect a hash shallowly, that is, it does not deeply compare the contents of supplied values (say a HASH reference, ARRAY reference, or some other opaque object).

This module was inspired by DBIx::Class::Row

Currently, setting, deleting keys or clearing the hash means that the object will lose history, so it will know that something has changed, but not if it is reset back at some later date:

    my ($object, $hash) = Hash::Dirty->new({ a => 1 });
    $object->is_dirty; # Nope

    $hash->{a} = 2;
    $object->is_dirty; # Yup
    $hash->{a} = 1;
    $object->is_dirty; # Yup, still dirty, even though the original value was 1


hash( <hash> )

Creates a new Hash::Dirty object and returns the tied hash reference, per Hash::Dirty->new.

If supplied, will use <hash> as the storage (initializing the object accordingly)


Hash::Dirty->new( <hash> )

Creates and returns a new Hash::Dirty object

If supplied, will use <hash> as the storage (initializing the object accordingly)

In list context, new will return both the object and the "regular" hash:

    my ($object, $hash) = Hash::Dirty->new;
    $hash->{a} = 1;
    $object->is_dirty; # Yup, it's dirty


Returns a reference to the overlying hash


Returns 1 if the hash is dirty at all, 0 otherwise

$object->is_dirty ( <key> )

Returns 1 if <key> is dirty, 0 otherwise

$object->is_dirty ( $key, $key, ..., )

Returns 1 if any <key> is dirty, 0 otherwise


Resets the hash to non-dirty status

This method affects the dirtiness only, it does not erase or alter the hash in anyway


Returns a hash indicating which keys are dirty

In scalar context, returns a hash reference


Returns a hash slice containg only the dirty keys and values

In scalar context, returns a hash reference


Returns a list of dirty keys


Returns a list of dirty values

$object->set( <key>, <value> )

$object->store( <key>, <value> )

$object->get( <key> )

$object->fetch( <key> )


$object->delete( <key> )


Robert Krimen, <rkrimen at>


Please report any bugs or feature requests to bug-hash-dirty at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc Hash::Dirty

You can also look for information at:



Copyright 2007 Robert Krimen, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: