1256a93a4Safresh1use 5.008001; 2256a93a4Safresh1 3256a93a4Safresh1use strict; 4256a93a4Safresh1use warnings; 5*f2a19305Safresh1use Test::More; 6b39c5158Smillertuse Text::Balanced qw ( extract_quotelike ); 7b39c5158Smillert 8*f2a19305Safresh1our $DEBUG; 9*f2a19305Safresh1sub debug { print "\t>>>",@_ if $DEBUG } 10*f2a19305Safresh1sub esc { my $x = shift||'<undef>'; $x =~ s/\n/\\n/gs; $x } 11b39c5158Smillert 12256a93a4Safresh1## no critic (BuiltinFunctions::ProhibitStringyEval) 13b39c5158Smillert 14256a93a4Safresh1my $cmd = "print"; 15256a93a4Safresh1my $neg = 0; 16256a93a4Safresh1my $str; 17b39c5158Smillertwhile (defined($str = <DATA>)) 18b39c5158Smillert{ 19b39c5158Smillert chomp $str; 20b39c5158Smillert if ($str =~ s/\A# USING://) { $neg = 0; $cmd = $str; next; } 21b39c5158Smillert elsif ($str =~ /\A# TH[EI]SE? SHOULD FAIL/) { $neg = 1; next; } 22b39c5158Smillert elsif (!$str || $str =~ /\A#/) { $neg = 0; next } 23b39c5158Smillert my $setup_cmd = ($str =~ s/\A\{(.*)\}//) ? $1 : ''; 24b39c5158Smillert my $tests = 'sl'; 25*f2a19305Safresh1 my $orig_str = $str; 26b39c5158Smillert $str =~ s/\\n/\n/g; 27b39c5158Smillert my $orig = $str; 28b39c5158Smillert 29b39c5158Smillert eval $setup_cmd if $setup_cmd ne ''; 30*f2a19305Safresh1 is $@, '', 'no error'; 31b39c5158Smillert if($tests =~ /l/) { 32b39c5158Smillert debug "\tUsing: $cmd\n"; 33b39c5158Smillert debug "\t on: [" . esc($setup_cmd) . "][" . esc($str) . "]\n"; 34b39c5158Smillert my @res; 35b39c5158Smillert eval qq{\@res = $cmd; }; 36*f2a19305Safresh1 is $@, '', 'no error'; 37b39c5158Smillert debug "\t got:\n" . join "", map { "\t\t\t$_: [" . esc($res[$_]) . "]\n"} (0..$#res); 38b39c5158Smillert debug "\t left: [" . esc($str) . "]\n"; 39b39c5158Smillert debug "\t pos: [" . esc(substr($str,pos($str))) . "...]\n"; 40*f2a19305Safresh1 ($neg ? \&isnt : \&is)->(substr($str,pos($str)||0,1), ';', "$orig_str matched list"); 41b39c5158Smillert } 42b39c5158Smillert 43b39c5158Smillert eval $setup_cmd if $setup_cmd ne ''; 44*f2a19305Safresh1 is $@, '', 'no error'; 45b39c5158Smillert if($tests =~ /s/) { 46b39c5158Smillert $str = $orig; 47b39c5158Smillert debug "\tUsing: scalar $cmd\n"; 48b39c5158Smillert debug "\t on: [" . esc($str) . "]\n"; 49256a93a4Safresh1 my $var = eval $cmd; 50b39c5158Smillert $var = "<undef>" unless defined $var; 51b39c5158Smillert debug "\t scalar got: [" . esc($var) . "]\n"; 52b39c5158Smillert debug "\t scalar left: [" . esc($str) . "]\n"; 53*f2a19305Safresh1 ($neg ? \&unlike : \&like)->( $str, qr/\A;/, "$orig_str matched scalar"); 54b39c5158Smillert } 55b39c5158Smillert} 56b39c5158Smillert 57b39c5158Smillert# fails in Text::Balanced 1.95 58b39c5158Smillert$_ = qq(s{}{}); 59b39c5158Smillertmy @z = extract_quotelike(); 60*f2a19305Safresh1isnt $z[0], ''; 61b39c5158Smillert 62*f2a19305Safresh1@z = extract_quotelike("<<, 1; done()\nline1\nline2\n\n and next"); 63*f2a19305Safresh1like $z[1], qr/\A,/, 'implied heredoc with ,' or do { 64*f2a19305Safresh1 diag "error: '$@'\ngot: ", explain \@z; 65*f2a19305Safresh1}; 66*f2a19305Safresh1 67*f2a19305Safresh1done_testing; 68b39c5158Smillert 69b39c5158Smillert__DATA__ 70b39c5158Smillert 71b39c5158Smillert# USING: extract_quotelike($str); 72b39c5158Smillert''; 73b39c5158Smillert""; 74b39c5158Smillert"a"; 75b39c5158Smillert'b'; 76b39c5158Smillert`cc`; 77b39c5158Smillert 78b39c5158Smillert<<EOHERE; done();\nline1\nline2\nEOHERE\n; next; 79b39c5158Smillert <<EOHERE; done();\nline1\nline2\nEOHERE\n; next; 80b39c5158Smillert<<"EOHERE"; done()\nline1\nline2\nEOHERE\n and next 81b39c5158Smillert<<`EOHERE`; done()\nline1\nline2\nEOHERE\n and next 82b39c5158Smillert<<'EOHERE'; done()\nline1\n'line2'\nEOHERE\n and next 83b39c5158Smillert<<'EOHERE;'; done()\nline1\nline2\nEOHERE;\n and next 84b39c5158Smillert<<" EOHERE"; done() \nline1\nline2\n EOHERE\nand next 85b39c5158Smillert<<""; done()\nline1\nline2\n\n and next 86b39c5158Smillert<<; done()\nline1\nline2\n\n and next 87b39c5158Smillert# fails in Text::Balanced 1.95 88b39c5158Smillert<<EOHERE;\nEOHERE\n; 89b39c5158Smillert# fails in Text::Balanced 1.95 90b39c5158Smillert<<"*";\n\n*\n; 91b39c5158Smillert 92b39c5158Smillert"this is a nested $var[$x] {"; 93b39c5158Smillert/a/gci; 94b39c5158Smillertm/a/gci; 95b39c5158Smillert 96b39c5158Smillertq(d); 97b39c5158Smillertqq(e); 98b39c5158Smillertqx(f); 99b39c5158Smillertqr(g); 100b39c5158Smillertqw(h i j); 101b39c5158Smillertq{d}; 102b39c5158Smillertqq{e}; 103b39c5158Smillertqx{f}; 104b39c5158Smillertqr{g}; 105b39c5158Smillertqq{a nested { and } are okay as are () and <> pairs and escaped \}'s }; 106b39c5158Smillertq/slash/; 107b39c5158Smillertq # slash #; 108b39c5158Smillertqr qw qx; 109b39c5158Smillert 110b39c5158Smillerts/x/y/; 111b39c5158Smillerts/x/y/cgimsox; 112b39c5158Smillerts{a}{b}; 113b39c5158Smillerts{a}\n {b}; 114b39c5158Smillerts(a){b}; 115b39c5158Smillerts(a)/b/; 116b39c5158Smillerts/'/\\'/g; 117b39c5158Smillerttr/x/y/; 118b39c5158Smillerty/x/y/; 119b39c5158Smillert 120b39c5158Smillert# fails on Text-Balanced-1.95 121b39c5158Smillert{ $tests = 'l'; pos($str)=6 }012345<<E;\n\nE\n 122b39c5158Smillert 123b39c5158Smillert# THESE SHOULD FAIL 124b39c5158Smillerts<$self->{pat}>{$self->{sub}}; # CAN'T HANDLE '>' in '->' 125b39c5158Smillerts-$self->{pap}-$self->{sub}-; # CAN'T HANDLE '-' in '->' 126b39c5158Smillert<<EOHERE; done();\nline1\nline2\nEOHERE;\n; next; # RDEL HAS NO ';' 127b39c5158Smillert<<'EOHERE'; done();\nline1\nline2\nEOHERE;\n; next; # RDEF HAS NO ';' 128b39c5158Smillert << EOTHERE; done();\nline1\nline2\n EOTHERE\n; next; # RDEL IS "" (!) 129