COOKBOOK

Practical examples of using Data::Hash::Patch::Smart.

Changing a deeply nested value

patch($data, [
    { op => 'change', path => '/a/b/c', to => 42 },
]);

Adding to an ordered array

patch($data, [
    { op => 'add', path => '/items/2', value => 'X' },
]);

Removing from an ordered array

patch($data, [
    { op => 'remove', path => '/items/1' },
]);

Unordered array operations

patch($data, [
    { op => 'add',    path => '/tags/*', value => 'new' },
    { op => 'remove', path => '/tags/*', from  => 'old' },
]);

Structural wildcard: update all users

patch($data, [
    { op => 'change', path => '/users/*/role', to => 'admin' },
]);

Structural wildcard: remove a field everywhere

patch($data, [
    { op => 'remove', path => '/**/deprecated' },
]);

Auto‑creating missing containers

patch({}, [
    { op => 'change', path => '/a/b/2', to => 'X' },
], create_missing => 1);

Produces:

{ a => { b => [ undef, undef, 'X' ] } }

Strict mode: catching mistakes

patch($data, [
    { op => 'change', path => '/missing/key', to => 1 },
], strict => 1);

Dies with:

Strict mode: missing hash key 'missing'

Wildcard + arrays of hashes

patch($data, [
    { op => 'change', path => '/servers/*/host', to => 'localhost' },
]);

Wildcard + deep nesting

patch($data, [
    { op => 'remove', path => '/root/*/*/debug' },
]);

Round‑trip with Data::Hash::Diff::Smart

my $changes = diff($old, $new);
my $patched = patch($old, $changes);

is_deeply($patched, $new);

Migrating configuration structures

Use diff + patch to migrate old config formats to new ones.

Bulk updates

patch($data, [
    { op => 'add', path => '/users/*/tags/*', value => 'active' },
]);

Removing empty containers

Combine wildcard remove with post‑processing to prune empty hashes.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 42:

Non-ASCII character seen before =encoding in 'Auto‑creating'. Assuming UTF-8