Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Format your source code into arbitrary shapes

by CubicSpline (Friar)
on Oct 14, 2001 at 00:22 UTC ( #118682=snippet: print w/replies, xml ) Need Help??
Description: Should you ever find yourself writing an obfu and want to make it pretty by formatting the code into a meaningful shape, it's really quite easy.

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.

# CubicSpline
# 2001/10/13
# Purpose: Format perl source file in a particular shape.
# Run: perl <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> )
    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
Replies are listed 'Best First'.
Re: Format your source code into arbitrary shapes
by blakem (Monsignor) on Oct 14, 2001 at 01:41 UTC
    This reminds me a bit of Acme::EyeDrops though that module actually munges your source into a non-alphanumeric encoding. You might want to take a look at it, just for ideas though.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://118682]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2021-06-19 13:25 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (92 votes). Check out past polls.