X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=samples%2Fhash2array.pl;h=d703ada917d6faf904b2d1d26472d82f5bb4bfac;hb=HEAD;hp=e70b69be8ea90e83c558a527dcc862c8a0beba67;hpb=68e31f8ce73ddedf82977b4e05ec550c1cfe5688;p=perl%2Fmodules%2Fautovivification.git diff --git a/samples/hash2array.pl b/samples/hash2array.pl index e70b69b..d703ada 100644 --- a/samples/hash2array.pl +++ b/samples/hash2array.pl @@ -3,8 +3,8 @@ use strict; use warnings; -use Fatal qw/open/; -use Text::Balanced qw/extract_bracketed/; +use Fatal qw; +use Text::Balanced qw; open my $hash_t, '<', 't/20-hash.t'; open my $array_t, '>', 't/30-array.t'; @@ -27,13 +27,48 @@ sub hash2array { sub dump_array { my ($a) = @_; + return 'undef' unless defined $a; - return $a unless ref $a; - die "Invalid argument" unless ref $a eq 'ARRAY'; - return '[ ' . join(', ', map dump_array($_), @$a) . ' ]'; + + if (ref $a) { + die "Invalid argument" unless ref $a eq 'ARRAY'; + return '[ ' . join(', ', map dump_array($_), @$a) . ' ]'; + } else { + $a = "'\Q$a\E'" if $a !~ /^\s*\d/; + return $a; + } +} + +sub extract ($$) { + extract_bracketed $_[0], $_[1], qr/.*?(?) { @@ -46,26 +81,31 @@ while (<$hash_t>) { print $array_t $_; print $array_fast_t $_; } else { - for my $file ([ 1, $array_t ], [ 0, $array_fast_t ]) { - local $_ = $_; - s!(\ba\b)?(\s*)HASH\b!($1 ? 'an': '') . "$2ARRAY"!eg; - s{ - {\s*(['"]?[a-z]['"]?(?:\s*,\s*['"]?[a-z]['"]?)*)\s*} - }{ - '[' . join(', ', map { my $n = num($_); $file->[0] ? "\$N[$n]" : $n } - split /\s*,\s*/, $1) . ']' - }gex; - s!%(\{?)\$!\@$1\$!g; - my $buf; - my $suffix = $_; - my ($bracket, $prefix); - while (do { ($bracket, $suffix, $prefix) = extract($suffix); $bracket }) { - $buf .= $prefix . dump_array(hash2array(eval $bracket)); - } - $buf .= $suffix; - $buf =~ s/\s+/ /g; - $buf =~ s/\s+$//; - print { $file->[1] } "$buf\n"; + print $array_t convert_testcase($_, 0); + print $array_fast_t convert_testcase($_, 1); + } +} + +close $hash_t; +close $array_t; +close $array_fast_t; + +open my $hash_kv_t, '<', 't/22-hash-kv.t'; +open my $array_kv_t, '>', 't/32-array-kv.t'; + +$in_data = 0; +while (<$hash_kv_t>) { + if (/^__DATA__$/) { + $in_data = 1; + } elsif (!$in_data) { + s{'%'}{'\@'}; + if (/\bplan\s*[\s\(]\s*tests\b/) { + s/\s*;?\s*$//; + s/^(\s*)//; + $_ = qq($1if ("\$]" >= 5.011) { $_ } else { plan skip_all => 'perl 5.11 required for keys/values \@array' }\n); } + } else { + $_ = convert_testcase($_, 1); } + print $array_kv_t $_; }