Create a format file made up of spaces (ASCII 32) and any other character to make your shape. When run, the code will be placed, character for character, in the format you designed. Spaces will remain spaces.
NOTE: This doesn't guarantee that the formatted code will run, but it will make it easier to fine-tune the formatted code.
# codeformat.pl # # CubicSpline # 2001/10/13 # # Purpose: Format perl source file in a particular shape. # # Run: perl codeformat.pl <perl source> <pattern file> use strict; use warnings; # SOURCE reads in the perl source code open SOURCE, $ARGV[0]; # PATTERN reads in the format text file open PATTERN, $ARGV[1]; # make a single string that contains entire source my $sourceline = join '', <SOURCE>; # strip out newlines and tabs and create character array, # reversed for popping. $sourceline =~ s/\n|\t//g; my @sourcechars = reverse split //,$sourceline; close SOURCE; my $output = ""; my $i; # read in pattern file line by line # for every non-space (ASCII 32) character # output the next char of the source string. while( <PATTERN> ) { chomp; my @patternchars = split //; for( $i = 0; $i <= $#patternchars;$i++) { $output .= $patternchars[$i] eq " " ? " " : pop @sourcechars; } $output .= "\n"; } close PATTERN; # print out the formatted source and any remaining # source that didn't fit into the pattern. print $output; if( scalar(@sourcechars) > 0 ) { print "\n\n",scalar(@sourcechars)," Remaining: \n\n", reverse @sour +cechars; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Format your source code into arbitrary shapes
by blakem (Monsignor) on Oct 14, 2001 at 01:41 UTC |