4 # Tests derived from Japhs.
9 print "1..0 # Skip: EBCDIC\n"; # For now, until someone has time.
19 # ./test.pl does real evilness by jumping to a label.
20 # This function copies the skip from ./test, omitting the goto.
24 my $n = @_ ? shift : 1;
27 print STDOUT "ok $test # skip: $why\n";
34 # ./test.pl doesn't give use 'notok', so we make it here.
37 my ($pass, $name, @mess) = @_;
38 _ok(!$pass, _where(), $name, @mess);
41 my $JaPH = "Just another Perl Hacker";
42 my $JaPh = "Just another Perl hacker";
43 my $JaPH_n = "Just another Perl Hacker\n";
44 my $JaPh_n = "Just another Perl hacker\n";
45 my $JaPH_s = "Just another Perl Hacker ";
46 my $JaPh_s = "Just another Perl hacker ";
47 my $JaPH_c = "Just another Perl Hacker,";
48 my $JaPh_c = "Just another Perl hacker,";
53 my $out = sprintf "Just another Perl Hacker";
59 my @primes = (2, 3, 7, 13, 53, 101, 557, 1429);
60 my @composites = (4, 10, 25, 32, 75, 143, 1333, 1728);
62 my %primeness = ((map {$_ => 1} @primes),
63 (map {$_ => 0} @composites));
65 while (my ($num, $is_prime) = each %primeness) {
66 my $comment = "$num is " . ($is_prime ? "prime." : "composite.");
68 my $sub = $is_prime ? "ok" : "notok";
70 &$sub (( 1 x $num) !~ /^1?$|^(11+?)\1+$/, $comment);
71 &$sub (( 0 x $num) !~ m 0^\0?$|^(\0\0+?)\1+$0, $comment);
72 &$sub (("m" x $num) !~ m m^\m?$|^(\m\m+?)\1+$mm, $comment);
77 { # Some platforms use different quoting techniques.
78 # I do not have access to those platforms to test
79 # things out. So, we'll skip things....
80 if ($^O eq 'MSWin32' ||
83 skip "Your platform quotes differently.", 3;
88 $expected =~ s/ /\n/g;
90 is (runperl (switches => [qw /'-weprint<<EOT;' -eJust -eanother
91 -ePerl -eHacker -eEOT/],
93 $expected, "Multiple -e switches");
95 is (runperl (switches => [q !'-wle$_=<<EOT;y/\n/ /;print;'!,
96 qw ! -eJust -eanother -ePerl -eHacker -eEOT!],
98 $JaPH . " \n", "Multiple -e switches");
100 is (runperl (switches => [qw !-wl!],
101 progs => [qw !print qq-@{[ qw+ Just
102 another Perl Hacker +]}-!],
104 $JaPH_n, "Multiple -e switches");
108 if ($^O eq 'MSWin32' ||
111 skip "Your platform quotes differently.", 1;
114 is (runperl (switches => [qw /-sweprint --/,
115 "-_='Just another Perl Hacker'"],
118 $JaPH, 'setting $_ via -s');
122 my $datafile = "datatmp000";
123 1 while -f ++ $datafile;
124 END {unlink_all $datafile}
126 open MY_DATA, "> $datafile" or die "Failed to open $datafile: $!";
127 print MY_DATA << " --";
135 close MY_DATA or die "Failed to close $datafile: $!\n";
142 if (/^#{7}(?:\s+(.*))?/) {
143 push @progs => {COMMENT => $1 || '',
151 elsif (/^(COMMENT|CODE|ARGS|SWITCHES|EXPECT|SKIP|SKIP_OS)
154 $progs [-1] {$key} = '' unless exists $progs [-1] {$key};
155 next unless defined $2;
162 if (ref ($progs [-1] {$key})) {
163 push @{$progs [-1] {$key}} => $_;
166 $progs [-1] {$key} .= $_;
170 foreach my $program (@progs) {
171 if (exists $program -> {SKIP}) {
172 chomp $program -> {SKIP};
173 skip $program -> {SKIP}, 1;
177 if (@{$program -> {SKIP_OS}}) {
178 chomp @{$program -> {SKIP_OS}};
179 if (grep {$^O eq $_} @{$program -> {SKIP_OS}}) {
180 skip "Your OS uses different quoting.", 1;
185 map {s/\$datafile/$datafile/} @{$program -> {ARGS}};
186 $program -> {EXPECT} = $JaPH unless exists $program -> {EXPECT};
187 $program -> {EXPECT} =~ s/\$JaPH_s\b/$JaPH_s/g;
188 $program -> {EXPECT} =~ s/\$JaPh_c\b/$JaPh_c/g;
189 $program -> {EXPECT} =~ s/\$JaPh\b/$JaPh/g;
190 chomp ($program -> {EXPECT}, @{$program -> {SWITCHES}},
191 @{$program -> {ARGS}});
192 fresh_perl_is ($program -> {CODE},
193 $program -> {EXPECT},
194 {switches => $program -> {SWITCHES},
195 args => $program -> {ARGS},
197 $program -> {COMMENT});
202 my $progfile = "progtmp000";
203 1 while -f ++ $progfile;
204 END {unlink_all $progfile}
206 my @programs = (<< ' --', << ' --');
207 #!./perl -- # No trailing newline after the last line!
208 BEGIN{$|=$SIG{__WARN__}=sub{$_=$_[0];y-_- -;print/(.)"$/;seek _,-open(_
209 ,"+<$0"),2;truncate _,tell _;close _;exec$0}}//rekcaH_lreP_rehtona_tsuJ
211 #!./perl -- # Remove trailing newline!
212 BEGIN{$SIG{__WARN__}=sub{$_=pop;y-_- -;print/".*(.)"/;
213 truncate$0,-1+-s$0;exec$0;}}//rekcaH_lreP_rehtona_tsuJ
218 # VMS needs extensions for files to be executable,
219 # but the Japhs above rely on $0 being exactly the
220 # filename of the program.
221 skip "VMS", 2 * @programs;
226 unless (defined $Config {useperlio}) {
227 skip "Uuseperlio", 2 * @programs;
232 foreach my $program (@programs) {
233 open my $fh => "> $progfile" or die "Failed to open $progfile: $!\n";
235 close $fh or die "Failed to close $progfile: $!\n";
237 chmod 0755 => $progfile or die "Failed to chmod $progfile: $!\n";
238 my $command = "./$progfile";
239 $command .= ' 2>&1' unless $^O eq 'MacOS';
240 my $output = `$command`;
243 is ($output, $JaPH, "Self correcting code $i");
245 $output = `$command`;
246 is ($output, "", "Self corrected code $i");
251 ####### Funky loop 1.
252 $_ = q ;4a75737420616e6f74686572205065726c204861636b65720as;;
253 for (s;s;s;s;s;s;s;s;s;s;s;s)
254 {s;(..)s?;qq qprint chr 0x$1 and \161 ssq;excess;}
256 ####### Funky loop 2.
257 $_ = q *4a75737420616e6f74686572205065726c204861636b65720a*;
258 for ($*=******;$**=******;$**=******) {$**=*******s*..*qq}
262 ####### Funky loop 3.
263 $_ = q *4a75737420616e6f74686572205065726c204861636b65720a*;
264 for ($*=******;$**=******;$**=******) {$**=*******s*..*qq}
268 ####### Funky loop 4.
269 $_ = q ?4a75737420616e6f74686572205065726c204861636b65720as?;??;
271 {??;s;(..)s?;qq ?print chr 0x$1 and \161 ss?;excess;??}
272 SKIP: Abuses a fixed bug.
274 ####### Funky loop 5.
275 for (s??4a75737420616e6f74686572205065726c204861636b65720as?;??;??)
276 {s?(..)s\??qq \?print chr 0x$1 and q ss\??excess}
277 SKIP: Abuses a fixed bug.
279 ####### Funky loop 6.
280 $a = q 94a75737420616e6f74686572205065726c204861636b65720a9 and
281 ${qq$\x5F$} = q 97265646f9 and s g..g;
282 qq e\x63\x68\x72\x20\x30\x78$&eggee;
283 {eval if $a =~ s e..eqq qprint chr 0x$& and \x71\x20\x71\x71qeexcess}
286 @r=reverse(M=>(0)x99=>CM=>(0)x399=>D=>(0)x99=>CD=>(
287 0)x299=>C=>(0)x9=>XC=>(0)x39=>L=>(0)x9=>XL=>(0)x29=>X=>IX=>0=>0=>0=>V=>IV=>0=>0
288 =>I=>$==-2449231+gm_julian_day+time);do{until($=<$#r){$_.=$r[$#r];$=-=$#r}for(;
289 !$r[--$#r];){}}while$=;$,="\x20";print+$_=>September=>MCMXCIII=>=>=>=>=>=>=>=>
293 SKIP: Times::JulianDay not part of the main distribution.
296 sub _'_{$_'_=~s/$a/$_/}map{$$_=$Z++}Y,a..z,A..X;*{($_::_=sprintf+q=%X==>"$A$Y".
297 "$b$r$T$u")=~s~0~O~g;map+_::_,U=>T=>L=>$Z;$_::_}=*_;sub _{print+/.*::(.*)/s};;;
298 *{chr($b*$e)}=*_'_;*__=*{chr(1<<$e)}; # Perl 5.6.0 broke this...
299 _::_(r(e(k(c(a(H(__(l(r(e(P(__(r(e(h(t(o(n(a(__(t(us(J())))))))))))))))))))))))
300 EXPECT: Just__another__Perl__Hacker
303 $"=$,;*{;qq{@{[(A..Z)[qq[0020191411140003]=~m[..]g]]}}}=*_=sub{print/::(.*)/};
304 $\=$/;q<Just another Perl Hacker>->();
307 $"=$,;*{;qq{@{[(A..Z)[qq[0020191411140003]=~m[..]g]]}}}=*_;
308 sub _ {push @_ => /::(.*)/s and goto &{ shift}}
309 sub shift {print shift; @_ and goto &{+shift}}
310 Hack ("Just", "Perl ", " ano", "er\n", "ther "); # YYYYMMDD
313 $, = " "; sub AUTOLOAD {($AUTOLOAD =~ /::(.*)/) [0];}
314 print+Just (), another (), Perl (), Hacker ();
316 ####### Look ma! No letters!
317 $@="\145\143\150\157\040\042\112\165\163\164\040\141\156\157\164".
318 "\150\145\162\040\120\145\162\154\040\110\141\143\153\145\162".
319 "\042\040\076\040\057\144\145\166\057\164\164\171";`$@`
322 ####### sprintf fun 1.
323 sub f{sprintf$_[0],$_[1],$_[2]}print f('%c%s',74,f('%c%s',117,f('%c%s',115,f(
324 '%c%s',116,f('%c%s',32,f('%c%s',97,f('%c%s',0x6e,f('%c%s',111,f('%c%s',116,f(
325 '%c%s',104,f('%c%s',0x65,f('%c%s',114,f('%c%s',32,f('%c%s',80,f('%c%s',101,f(
326 '%c%s',114,f('%c%s',0x6c,f('%c%s',32,f('%c%s',0x48,f('%c%s',97,f('%c%s',99,f(
327 '%c%s',107,f('%c%s',101,f('%c%s',114,f('%c%s',10,)))))))))))))))))))))))))
329 ####### sprintf fun 2.
330 sub f{sprintf'%c%s',$_[0],$_[1]}print f(74,f(117,f(115,f(116,f(32,f(97,
331 f(110,f(111,f(116,f(104,f(0x65,f(114,f(32,f(80,f(101,f(114,f(0x6c,f(32,
332 f(0x48,f(97,f(99,f(107,f(101,f(114,f(10,q ff)))))))))))))))))))))))))
335 %0=map{local$_=$_;reverse+chop,$_}ABC,ACB,BAC,BCA,CAB,CBA;$_=3 .AC;1while+
336 s/(\d+)((.)(.))/($0=$1-1)?"$0$3$0{$2}1$2$0$0{$2}$4":"$3 => $4\n"/xeg;print
391 -Mstrict='}); print "Just another Perl Hacker"; ({'
398 srand 123456;$-=rand$_--=>@[[$-,$_]=@[[$_,$-]for(reverse+1..(@[=split
399 //=>"IGrACVGQ\x02GJCWVhP\x02PL\x02jNMP"));print+(map{$_^q^"^}@[),"\n"
400 SKIP: Solaris specific.
402 ####### print and __PACKAGE__
403 package Just_another_Perl_Hacker; sub print {($_=$_[0])=~ s/_/ /g;
404 print } sub __PACKAGE__ { &
405 print ( __PACKAGE__)} &
410 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
411 / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
412 % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %;
413 BEGIN {% % = ($ _ = " " => print "Just another Perl Hacker\n")}
416 sub J::FETCH{Just }$_.='print+"@{[map';sub J::TIESCALAR{bless\my$J,J}
417 sub A::FETCH{another}$_.='{tie my($x),$';sub A::TIESCALAR{bless\my$A,A}
418 sub P::FETCH{Perl }$_.='_;$x}qw/J A P';sub P::TIESCALAR{bless\my$P,P}
419 sub H::FETCH{Hacker }$_.=' H/]}\n"';eval;sub H::TIESCALAR{bless\my$H,H}
422 package Z;use overload'""'=>sub{$b++?Hacker:another};
423 sub TIESCALAR{bless\my$y=>Z}sub FETCH{$a++?Perl:Just}
424 $,=$";my$x=tie+my$y=>Z;print$y,$x,$y,$x,"\n";#Abigail
428 sub A::TIESCALAR{bless\my$x=>A};package B;@q[0..3]=qw/Hacker Perl
429 another Just/;use overload'""'=>sub{pop @q};sub A::FETCH{bless\my
430 $y=>B}; tie my $shoe => qq 'A';print "$shoe $shoe $shoe $shoe\n";
433 sub A::TIESCALAR{bless\my$x=>'A'};package B;@q=qw/Hacker Perl
434 another Just/;use overload'""',sub{pop @q};sub A::FETCH{bless
435 \my $y=>B};tie my$shoe=>'A';print"$shoe $shoe $shoe $shoe\n";
438 tie $" => A; $, = " "; $\ = "\n"; @a = ("") x 2; print map {"@a"} 1 .. 4;
439 sub A::TIESCALAR {bless \my $A => A} # Yet Another silly JAPH by Abigail
440 sub A::FETCH {@q = qw /Just Another Perl Hacker/ unless @q; shift @q}
441 SKIP: Pending a bug fix.
443 ####### Prototype fun 1
444 sub camel (^#87=i@J&&&#]u'^^s]#'#={123{#}7890t[0.9]9@+*`"'***}A&&&}n2o}00}t324i;
445 h[{e **###{r{+P={**{e^^^#'#i@{r'^=^{l+{#}H***i[0.9]&@a5`"':&^;&^,*&^$43##@@####;
446 c}^^^&&&k}&&&}#=e*****[]}'r####'`=437*{#};::'1[0.9]2@43`"'*#==[[.{{],,,1278@#@);
447 print+((($llama=prototype'camel')=~y|+{#}$=^*&[0-9]i@:;`"',.| |d)&&$llama."\n");
448 SKIP: Abuses a fixed bug.
450 ####### Prototype fun 2
451 print prototype sub "Just another Perl Hacker" {};
453 ####### Prototype fun 3
454 sub _ "Just another Perl Hacker"; print prototype \&_
458 ${"@_"} = "/"; split // => eval join "+" => 1 .. 7;
459 *{"@_"} = sub {foreach (sort keys %_) {print "$_ $_{$_} "}};
460 %{"@_"} = %_ = (Just => another => Perl => Hacker); &{%{%_}};
464 $" = "/"; split // => eval join "+" => 1 .. 7;
465 *{"@_"} = sub {foreach (sort keys %_) {print "$_ $_{$_} "}};
466 %_ = (Just => another => Perl => Hacker); &{%_};
470 $" = "/"; split $, => eval join "+" => 1 .. 7;
471 *{"@_"} = sub {foreach (sort keys %_) {print "$_ $_{$_} "}};
472 %{"@_"} = %_ = (Just => another => Perl => Hacker); &{%{%_}};
475 ####### Here documents 1
476 $_ = "\x3C\x3C\x45\x4F\x54"; s/<<EOT/<<EOT/e; print;
477 Just another Perl Hacker
480 ####### Here documents 2
481 $_ = "\x3C\x3C\x45\x4F\x54";
482 print if s/<<EOT/<<EOT/e;
483 Just another Perl Hacker
486 ####### Here documents 3
487 $_ = "\x3C\x3C\x45\x4F\x54" and s/<<EOT/<<EOT/e and print;
488 Just another Perl Hacker
491 ####### Here documents 4
492 $_ = "\x3C\x3C\x45\x4F\x54\n" and s/<<EOT/<<EOT/ee and print;
493 "Just another Perl Hacker"
496 ####### Self modifying code 1
497 $_ = "goto F.print chop;\n=rekcaH lreP rehtona tsuJ";F1:eval
500 ####### Overloaded constants 1
501 BEGIN {$^H {q} = sub {pop and pop and print pop}; $^H = 2**4.2**12}
502 "Just "; "another "; "Perl "; "Hacker";
504 ####### Overloaded constants 2
505 BEGIN {$^H {q} = sub {$_ [1] =~ y/S-ZA-IK-O/q-tc-fe-m/d; $_ [1]}; $^H = 0x28100}
506 print "Just another PYTHON hacker\n";
509 ####### Overloaded constants 3
510 BEGIN {$^H {join "" => ("a" .. "z") [8, 13, 19, 4, 6, 4, 17]} = sub
511 {["", "Just ", "another ", "Perl ", "Hacker\n"] -> [shift]};
512 $^H = hex join "" => reverse map {int ($_ / 2)} 0 .. 4}
515 ####### Overloaded constants 4
516 BEGIN {$^H {join "" => ("a" .. "z") [8, 13, 19, 4, 6, 4, 17]} = sub
517 {["", "Just ", "another ", "Perl ", "Hacker"] -> [shift]};
518 $^H = hex join "" => reverse map {int ($_ / 2)} 0 .. 4}
519 print 1, 2, 3, 4, "\n";
521 ####### Overloaded constants 5
522 BEGIN {my $x = "Knuth heals rare project\n";
523 $^H {integer} = sub {my $y = shift; $_ = substr $x => $y & 0x1F, 1;
524 $y > 32 ? uc : lc}; $^H = hex join "" => 2, 1, 1, 0, 0}
525 print 52,2,10,23,16,8,1,19,3,6,15,12,5,49,21,14,9,11,36,13,22,32,7,18,24;
528 print v74.117.115.116.32;
529 print v97.110.111.116.104.101.114.32;
530 print v80.101.114.108.32;
531 print v72.97.99.107.101.114.10;
534 print 74.117.115.116.32;
535 print 97.110.111.116.104.101.114.32;
536 print 80.101.114.108.32;
537 print 72.97.99.107.101.114.10;
540 print v74.117.115.116.32, v97.110.111.116.104.101.114.32,
541 v80.101.114.108.32, v72.97.99.107.101.114.10;
544 print 74.117.115.116.32, 97.110.111.116.104.101.114.32,
545 80.101.114.108.32, 72.97.99.107.101.114.10;
548 print v74.117.115.116.32.97.110.111.116.104.101.114.
549 v32.80.101.114.108.32.72.97.99.107.101.114.10;
552 print 74.117.115.116.32.97.110.111.116.104.101.114.
553 32.80.101.114.108.32.72.97.99.107.101.114.10;
555 ####### Symbolic references.
556 map{${+chr}=chr}map{$_=>$_^ord$"}$=+$]..3*$=/2;
557 print "$J$u$s$t $a$n$o$t$h$e$r $P$e$r$l $H$a$c$k$e$r\n";
562 $; # The return of the lone dollar?
563 {Just=>another=>Perl=>Hacker=>} # Bare block?
565 print%; # No right operand for %?
568 @;=split//=>"Joel, Preach sartre knuth\n";$;=chr 65;%;=map{$;++=>$_}
569 0,22,13,16,5,14,21,1,23,11,2,7,12,6,8,15,3,19,24,14,10,20,18,17,4,25
574 $;=$";$;{Just=>another=>Perl=>Hacker=>}=$/;print%;
577 $_ = "\112\165\163\1648\141\156\157\164\150\145\1628\120\145"
578 . "\162\1548\110\141\143\153\145\162\0128\177" and &japh;
579 sub japh {print "@_" and return if pop; split /\d/ and &japh}
582 sub _ {$_ = shift and y/b-yB-Y/a-yB-Y/ xor !@ _?
584 print and push @_ => shift and goto &{(caller (0)) [3]}}
585 split // => "KsvQtbuf fbsodpmu\ni flsI " xor & _
588 :$:=~s:$":Just$&another$&:;$:=~s:
589 :Perl$"Hacker$&:;chop$:;print$:#:
593 -:;another Perl Hacker
601 -:;another Perl Hacker
603 $:;$:=~y:;::d;print+Just.$:
606 s[$,][join$,,(split$,,($!=85))[(q[0006143730380126152532042307].
607 q[41342211132019313505])=~m[..]g]]e and y[yIbp][HJkP] and print;
608 SKIP: Platform dependent.
611 eval {die ["Just another Perl Hacker"]}; print ${$@}[$#{@${@}}]
614 eval {die ["Just another Perl Hacker\n"]}; print ${$@}[$#{@${@}}]
617 eval {die ["Just another Perl Hacker"]}; print ${${@}}[$#{@{${@}}}]
620 eval {die ["Just another Perl Hacker\n"]}; print ${${@}}[$#{@{${@}}}]
623 eval {die [[qq [Just another Perl Hacker]]]};; print
624 ${${${@}}[$#{@{${@}}}]}[$#{${@{${@}}}[$#{@{${@}}}]}]
626 ####### Closure returning itself.
627 $_ = "\nrekcaH lreP rehtona tsuJ"; my $chop; $chop = sub {print chop; $chop};
628 $chop -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
629 -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
631 ####### Special blocks 1
632 BEGIN {print "Just " }
633 CHECK {print "another "}
634 INIT {print "Perl " }
635 END {print "Hacker\n"}
637 ####### Special blocks 2
638 END {print "Hacker\n"}
639 INIT {print "Perl " }
640 CHECK {print "another "}
641 BEGIN {print "Just " }
643 ####### Recursive regex.
644 my $qr = qr/^.+?(;).+?\1|;Just another Perl Hacker;|;.+$/;
648 ####### use lib 'coderef'
649 use lib sub {($\) = split /\./ => pop; print $"};
650 eval "use Just" || eval "use another" || eval "use Perl" || eval "use Hacker";
652 Just another Perl Hacker