File: callback_aliasing.t

package info (click to toggle)
libdata-visitor-perl 0.30-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 204 kB
  • ctags: 49
  • sloc: perl: 1,169; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 717 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;


use Data::Visitor::Callback;

foreach my $ignore ( 0, 1 ) {
	my $structure = {
		foo => "bar",
		gorch => [ "baz", 1 ],
	};

	my $o = Data::Visitor::Callback->new(
		ignore_return_values => $ignore,
		plain_value => sub { no warnings 'uninitialized'; s/b/m/g; "laaa" },
		array => sub { $_ = 42; undef },
	);

	$_ = "original";

	$o->visit( $structure );

	is( $_, "original", '$_ unchanged in outer scope');

	is_deeply( $structure, {
		foo => "mar",
		gorch => 42,
	}, "values were modified" );

	$o->callbacks->{hash} = sub { $_ = "value" };
	$o->visit( $structure );
	is( $structure, "value", "entire structure can also be changed");
}

done_testing;