1# Data::Walk - Traverse Perl data structures. 2# Copyright (C) 2005-2016 Guido Flohr <guido.flohr@cantanea.com>, 3# all rights reserved. 4 5# This program is free software; you can redistribute it and/or modify it 6# under the terms of the GNU Library General Public License as published 7# by the Free Software Foundation; either version 2, or (at your option) 8# any later version. 9 10# This program is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13# Library General Public License for more details. 14 15# You should have received a copy of the GNU Library General Public 16# License along with this program; if not, write to the Free Software 17# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 18# USA. 19 20use strict; 21 22use Test; 23use Data::Walk; 24 25BEGIN { 26 plan tests => 11; 27} 28 29my ($data, $wanted, $count, $preprocess); 30 31$data = { foo => 'bar' }; 32$data->{baz} = $data; 33 34$count = 0; 35$wanted = sub { 36 ++$count; 37 ok ($count <= 5); 38}; 39walk { wanted => $wanted }, $data; 40 41ok $count, 5; 42 43$preprocess = sub { 44 my @args = @_; 45 46 return () if $count > 10; 47 48 return @args; 49}; 50 51$wanted = sub { 52 ++$count; 53}; 54walk { wanted => $wanted, 55 follow => 1, 56 preprocess => $preprocess, 57 }, $data; 58ok $count > 5; 59 60$data = {}; 61bless $data, 'Data::Walk::Fake'; 62 63$wanted = sub { 64 ok $Data::Walk::address, int $_; 65}; 66walk { wanted => $wanted }, $data; 67 68my $scalar = 'foobar'; 69$data = [ \$scalar, \$scalar, \$scalar ]; 70$count = 0; 71$wanted = sub { 72 unless ('ARRAY' eq ref $_) { 73 ok $Data::Walk::seen, $count++; 74 } 75}; 76walk { wanted => $wanted }, $data; 77$count, scalar @{$data}; 78