Acme::Palindrome - Programs are the same backward and forward


Acme-Palindrome documentation Contained in the Acme-Palindrome distribution.

Index


Code Index:

NAME

Top

Acme::Palindrome - Programs are the same backward and forward

SYNOPSIS

Top

  use Acme::Palindrome;

  print "Hello world";

DESCRIPTION

Top

The first time you run a program under use Acme::Palindrome, the module reverses the code in your source file from top to bottom, left to right. The code continues to work exactly as it did before, but now it looks like this:

  use Acme::Palindrome;




  ;"dlrow olleH" tnirp

DIAGNOSTICS

Top

Acme::Palindrome could not access the source file to modify it.

SEE ALSO

Top

Acme::Bleach - Code and documentation nearly taken verbatim.

AUTHOR

Top

Casey West <casey@geeknest.com>

COPYRIGHT

Top


Acme-Palindrome documentation Contained in the Acme-Palindrome distribution.

package Acme::Palindrome;
# $Id: $
use strict qw[vars subs];

use vars qw[$VERSION $TIE];
$VERSION = (qw$Revision: 0.1$)[1];
$TIE = " \t"x8;

sub backward { "$TIE\n".palindrome(split "\n", $_[0])."\n" }
sub forward  { palindrome(split "\n", $_[0]) }

sub is_forward  { $_[0] !~ /^$TIE/ }
sub is_backward { $_[0] =~ /^$TIE/ }

open 0 or print "Can't reverse '$0'\n" and exit;
(my $code = join "", <0>) =~ s/.*^\s*use\s+Acme::Palindrome\s*;\n//sm;
local $SIG{__WARN__} = \&is_forward;
do {eval forward $code; exit} if is_backward $code;
open 0, ">$0" or print "Cannot reverse '$0'\n" and exit;
print {0} "use Acme::Palindrome;\n", backward $code and exit;

sub palindrome {
    my $max = 0;
    length > $max && ( $max = length ) for @_;
    return join "\n",
      map sprintf( "%${max}s", scalar reverse $_ ),
	reverse @_;
}

1;

__END__