Acme-AsciiArtinator
view release on metacpan or search on metacpan
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
}
} continue {
$ntest++;
}
###############################################
my $max_tries = $opts{"retry"} || 100;
my @tokens = &asciiindex_code($CODE);
my @contexts = @asciiartinate::contexts;
my @blocks = &asciiindex_art($PIC);
my $ipad;
for ($ipad = 0; $ipad < $max_tries; $ipad++) {
print "\n\n\n\nPad try # $ipad\n\n\n\n";
my ($newt,$newc) = &pad(\@tokens, \@contexts, \@blocks);
if (defined $newc) {
for (my $i=0; $i<@$newt; $i++) {
print $newt->[$i], "\t", $newc->[$i], "\n";
}
@tokens = @$newt;
if ($opts{"filler"} != 0) {
&tweak_padding($opts{"filler"}, \@tokens, \@contexts);
}
print_code_to_pic($PIC, @tokens);
my $fh;
open($fh, ">", $OUTPUT);
select $fh;
print_code_to_pic($PIC, @tokens);
select STDOUT;
close $fh;
my $c1 = &compile_check($OUTPUT);
if ($c1 > 0) {
croak "Artinated code does not compile! Darn.\n";
exit $c1 >> 8;
}
##################################################
#
# artination complete
#
##################################################
open($fh,"<", $OUTPUT);
my @output = <$fh>;
close $fh;
# test output
#
# make sure artinated code produces same outputs
# as the original code on the test cases.
#
$ntest = 1;
if (defined $opts{"test_argv1"}) {
print "Running post-tests on artinated code\n";
}
while (defined $opts{"test_argv$ntest"} || defined $opts{"test_input$ntest"}) {
my (@test_argv, @test_stdin) = ();
print "Testing output # $ntest:\n";
@test_argv = @{$opts{"test_argv$ntest"}} if defined $opts{"test_argv$ntest"};
@test_stdin = @{$opts{"test_input$ntest"}} if defined $opts{"test_input$ntest"};
my $fh;
next if !defined $Acme::AsciiArtinator::TestOutput[$ntest];
my $output = "";
if (defined $opts{"test_input$ntest"}) {
open($fh, ">", "ascii-art-test-$ntest-$$.stdin");
print $fh @test_stdin;
close $fh;
$output = qx{$^X "$OUTPUT" @test_argv < ascii-art-test-$ntest-$$.stdin};
unlink "ascii-art-test-$ntest-$$.stdin";
} else {
$output = qx{$^X "$OUTPUT" @test_argv};
}
print "Ran post-test # $ntest with argv: \"@test_argv\", stdin: \"@test_stdin\"\n";
if ($output eq $Acme::AsciiArtinator::TestOutput[$ntest]) {
print "Post-test # $ntest: PASS\n";
$Acme::AsciiArtinator::TestResult[$ntest] = "PASS";
} else {
print "Post-test # $ntest: FAIL\n";
$Acme::AsciiArtinator::TestResult[$ntest] = "FAIL";
print STDERR "-- " x 13, "\n";
print STDERR "Original results for test # $ntest:\n";
print STDERR "-- " x 7, "\n";
print STDERR $Acme::AsciiArtinator::TestOutput[$ntest];
print STDERR "\n", "-- " x 13, "\n";
print STDERR "Final results for test # $ntest:\n";
print STDERR $output;
print STDERR "\n", "-- " x 13, "\n\n";
}
} continue {
$ntest++;
}
return @output;
}
}
if ($ipad >= $max_tries) {
croak "The ASCII Artinator was unable to embed your code in the picture ",
"after $max_tries tries.\n";
}
}
#
# run a file containing Perl code for a Perl compilation check
#
sub compile_check {
my ($file) = @_;
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
If this works (and it might not, for a variety of
reasons), we will get a new file called C<spider-art.pl>
that looks something like:
& I
() ;$
N= <>
;; ;;
;; ;;
;; ;
;; ;
;; ;; ;; ;
;; ;; ;; ;;
;; ;; ;@ o=
( map {$z =$
{U }x( $x= 1+
$N- $_) ;' 'x $x. ($".
$F)x$_ .($B.$z.$ P. $z.$F).
($B.$")x$_.$/}0..$N);@
o=(@o,($U.$F)x++$N.($"x3).($B.$U
)x$N.$/);;;;print@o;;;sub I{( $B,
$F, $P,$U)=qw(\\ /
| _);;}while($_=pop @o
){ y'/\\'\/';;;@o||y#_# #;; ;;;
;$ t++ ||y#_ # _#;print }# ##
## ## ################ ## ##
# ## ################ # #
# ## ################ ## #
# ## ############## ##
## ############ ##
## ######## ##
## ##
### ###
## ###
## ##
Hey, that was pretty cool! Let's see if it works.
$ echo 6 | perl spider-art.pl
\_______|_______/
/\______|______/\
/ /\_____|_____/\ \
/ / /\____|____/\ \ \
/ / / /\___|___/\ \ \ \
/ / / / /\__|__/\ \ \ \ \
/ / / / / /\_|_/\ \ \ \ \ \
_/_/_/_/_/_/_/ \_\_\_\_\_\_\_
\ \ \ \ \ \ \___/ / / / / / /
\ \ \ \ \ \/_|_\/ / / / / /
\ \ \ \ \/__|__\/ / / / /
\ \ \ \/___|___\/ / / /
\ \ \/____|____\/ / /
\ \/_____|_____\/ /
\/______|______\/
/ | \
=head1 UNDER THE HOOD
To fill in the shape of the spider, we inserted whitespace,
semi-colons, sharps, and maybe the occasional C<{> C<}> pair
into the original code. Certain blocks of text, like
C<print>, C<while>, and C<y#_ # _#> are kept intact since
splitting them would cause the program to either fail to
compile or to behave differently.
The ASCII Artinator tokenizes the code and
does its best to identify
=over 4
=item 1. Character strings that must not be divided
These include alphanumeric literals, quoted strings,
and most regular expressions.
=item 2. Places in the code where it is OK to insert padding.
=item 3. Places in the ASCII artwork where there are multiple
consecutive darkspace characters
=back
The next step is to try to align the tokens from the code
with enough contiguous blocks of darkspace in the art. When
a token is misaligned, we attempt to align it by inserting
some padding at some point in the code before that token.
There are currently two ways that we pad the code. Each
time there is a need to pad the code, we randomly choose
a padding method and randomly choose an eligible position
for padding.
=over 4
=item 1. Inserting semi-colons at the beginning or end of a statement
In general, we can put as many semi-colons as we like at the beginning
or end of statements. The following lines of code should all do the
same thing:
$a=$b+$c;$d=4
$a=$b+$c;;;;;;$d=4;;;;;;
;;;;;;;;;$a=$b+$c;;;;;;;;$d=4;
=item 2. Putting braces around a variable name.
In general, we can replace C<$name> with C<${name}> and the code
will run the same.
=back
There are several other interesting ways to pad code (putting parentheses
around expressions, adding and or-ing zeros to expressions, using quoted
strings in a void context) that may be put to use in future versions
of this module.
When all tokens from the code are successfully aligned with the
blocks of darkspace from the artwork, we can paste the code on top
of the art and write the output file.
Sometimes we insert too many characters without successfully
aligning the tokens and darkspace blocks (and actually in the
spider example, this happens about 90% of the time). If this
happens, we will start over and retry up to 100 times.
=head1 BEST PRACTICES
Certain coding practices will increase the chance that
C<Acme::AsciiArtinator> will be able to embed your code
in the artwork of your choice. In no particular order,
here are some suggestions:
=over 4
=item * Make sure the original code works
Make sure the code compiles and test it to see if it
works like you expect it to
before running the ASCII Artinator. It would be frustrating to
try to debug an artinated script only to later realize that
there was some bug in the original input.
=item * Get rid of comments
This module won't handle comments very well. There's no way
to stop the ASCII Artinator from splitting your comment across
two lines and breaking the code.
=item * Reduce whitespace
In addition to making the code longer and thus more difficult
to align, any whitespace in your code will be printed out as
space over a darkspace in the art and put a "hole" in your
picture. It would be nice if there was a way to align the
whitespace in the code with the whitespace in the art, but that
is probably something for a far future version.
=item * Avoid significant newlines
Newlines are stripped from the code before the code is tokenized.
If there are any significant newlines (I mean the literal 0x0a char.
It should still be OK to say C<print"\n">), then the artinated
code will run differently.
=item * Consider workarounds for quoted strings
Quoted strings are parsed as a single token. Consider ways to break
them up so that can be split into multiple tokens. For example, instead
of saying C<$h="Hello, world!";>, we could actually say something like:
&I;($c,$e)=qw(, !);$h=H.e.l.l.o.$c.$".W.o.r.l.d.$e;
The modified code is a lot longer, but this code can be split at any
point except in the middle of C<qw>, so it is much more flexible code
from the perspective of the Artinator.
=item * Perform some smart reordering
In the spider example, we see that the largest contiguous blocks of
darkspace are in the center of the spider, and at the beginning and
end of the spider art, there are many smaller blocks of darkspace.
In this case, code that has large tokens in the middle or near the
end of the code will be more flexible than code with large tokens in
the beginning of the code. So for example, we are better off
writing
@o=(map ... );print@o
than
print@o=(map ... )
even through the latter code is a little shorter.
=back
=head1 OPTIONS
The C<asciiartinate> method supports the following options:
=over 4
=item art_file => filename
=item art_string => string
=item art => string
Specifies the ASCII artwork that we'll try to embed code into.
At least one of C<art>, C<art_string>, C<art_file> must be
specified.
=item code_file => filename
=item code_string => string
=item code => string
Specifies the Perl code that we will try to embed into the
art. At least one of C<code>, C<code_string>, C<code_file>
must be specified.
=item output => filename
Specifies the output file for the embedded code. If omitted,
output is written to the file "ascii-art.pl" in the current
directory.
=item compile_check => 0 | 1
Runs the Perl interpreter with the C<-cw> flags on the
original code string and asserts that the code compiles.
=item debug => 0 | 1
Causes the ASCII Artinator to display verbose messages
about what it is trying to do while it is doing what it
is trying to do.
=item test_argv1 => [ @args ], test_argv2 => [ @args ] , test_argv3 => ...
Executes the original and the artinated code and compares the output
to make sure that the artination process did not change the
behavior of the code. A separate test will be conducted for
every C<test_argvE<lt>NNNE<gt>> parameter passed to the
C<asciiartinate> method. The arguments associated with each
parameter will be passed to the code as command-line arguments.
=item test_input1 => [ @data ], test_input2 => [ @data ], test_input3 => ...
Executes the original and the artinated code and compares the output
to make sure that the artination process did not change the
behavior of the code. A separate test will be conducted for
every C<test_inputE<lt>NNNE<gt>> parameter passed to the
C<asciiartinate> method. The data associated with each
parameter will be passed to the standard input of the code.
=back
=head1 TODO
Lots of future enhancements are possible:
=over 4
=item * Use new ways of padding code
=back
=over 4
=item * Take big blocks of filler and fill them with something else. Random quoted strings.
=back
=over 4
=item * Try to align whitespace in the code with whitespace in the art.
=back
=over 4
=item * Have a concept of "grayspace" in the artwork. These are positions where
we can put either whitespace or a character from the code, whichever makes it
easier to align the code.
=back
=over 4
=item * Optionally implement some "best practices" automatically to make the code
more flexible without changing its behavior.
=back
=head1 BUGS
Probably lots.
=head1 SEE ALSO
If you liked this module, you might also get a kick out of L<Acme::EyeDrops>.
=head1 AUTHOR
Marty O'Brien, E<lt>mob@cpan.orgE<gt>
=head1 LICENSE AND COPYRIGHT
( run in 1.368 second using v1.01-cache-2.11-cpan-800906f7e73 )