<?xml version="1.0" encoding="windows-1252"?>
<node id="128986" title="Reusing camel code" created="2001-12-02 14:43:17" updated="2005-08-13 08:36:43">
<type id="115">
perlquestion</type>
<author id="127116">
gmax</author>
<data>
<field name="doctext">
Dear monks,&lt;br&gt;
This one was a slow Saturday, and I spent some idle time looking
at the [id:=//45213|Camel Code]. It would be nice to write such a program with
a different shape, I thought. Unfortunately, among my many abilities,
drawing is definitely missing. However, since the Camel code is
a self drawing program, I thought that its magic, albeit not 
easily duplicated, could be reused. Thus, I got hold of some shapes
(using a separated parser, which skips the first two lines and the 
__DATA__ section) and managed to re-use the Camel code, 
according to the golden principle of Laziness, so well explained by 
Larry Wall.
&lt;br&gt;
In order to run this program, you must have the Camel code in your current directory. (I only tried it in Linux and FreeBSD.)&lt;br&gt;
&lt;code&gt;perl camelfilter.pl  # shows the llama
perl camelfilter.pl shark
perl camelfilter.pl gmax &lt;/code&gt;
The patterns for [id://1597|shark] and [id://108730|llama] are taken from the omonimous files
in the obfuscated code section of www.perlmonks.net. A third pattern,
a rather unimaginative "gmax", I got from rotating the output of the
good old Unix &lt;I&gt;banner&lt;/I&gt; program (still I can't draw, as I told you before).
Parsing these files, I got my patterns as sets of F (for Filled) or S (for
Spaces) followed by the number of occurrences. Such patterns are then
assigned to scalar variables in camelfilter.pl. Newlines are indicated
by colons. Thus a pattern of "S30F12:" means 30 spaces, followed by 12 
nonspaces and a newline. 
&lt;code&gt;
  +------------------+
  |                  |
  |  while pattern   |&lt;------------------+  &lt;-----------------+
  |                  |                   |                    |
  +------------------+                   |                    |
           |                             |                    |
           V                             |                    |
          /\                             |                    |
         /  \                 +----------+----------+         |
        /    \                |                     |         |
       / is it\____NO_______&gt; | write next non space|         |
       \ space/               | characters from     |         |
        \  ? /                | camel.pl            |         |
         \  /                 +---------------------+         |
          \/                                                  |
           |                                                  |
           |                                                  |
          YES                                                 |
   	    |                                                  |
   	    V                                                  |
   +---------------------+                                    |
   | write as many spaces|                                    |
   | as stated in the    +------------------------------------+
   | pattern             |
   +---------------------+
&lt;/code&gt;
camelfilter.pl reads camel.pl and makes a string of its code,
skipping all spaces. The code is then rewritten in newcamel.pl
following the pattern of llama (default),  shark or gmax (if 
stated as an argument in the command line).
&lt;br&gt;
newcamel.pl will inherit camel.pl's magic and print its new shapes, 
with same additional noise lines (due to the difference in size 
between camels, llamas, sharks and gmax's, I suppose).
You can either get rid of the noise by filtering the unwanted lines 
&lt;code&gt;
system "perl $newcamel | perl -ne 'print if !/^ ?mm|[\\^]{20}/'";&lt;/code&gt;
or show them 
&lt;code&gt;
do "$newcamel";
&lt;/code&gt;
There is room for improvement (patterns could be bit-encoded,
for instance), but &lt;I&gt;I would like some specific hints on
how to apply the pattern in a more linear way than my &lt;B&gt;while ($$pattern ...)&lt;/B&gt;&lt;/I&gt;. I suspect that 
it might be a mappish solution somewhere, but my Laziness did not 
come up with any practical suggestion (too much Impatience?, not
enough Hybris? I don't know.)&lt;br&gt;
I am working on patterns for a less entertaining project, involving
database results representation, and some problems are very similar
to this one: applying a pattern to a long stream of data in order
to create a complex report. 
Since the problem is basically the same, I thought that asking
with a funny example would at least make someone smile.
&lt;br&gt;
TIAFYH
&lt;br&gt;
gmax
&lt;code&gt;

#!/usr/bin/perl -w
# camelfilter.pl
use strict;
my $llama = "S51F11:S47F10S1F9:S45F21:S49F12:S49F9:S49F9:S49F9:"
. "S49F9:S49F9:S47F11:S3F8S4F26S4F13:S1F57:F57:F57:S2F54:S2F54:"
. "S3F51:S4F49:S4F48:S5F45:S6F17S3F23:S7F15S6F20:S8F13S10F16:"
. "S10F10S14F12:S11F8S17F10:S12F5S20F4S1F4:S12F4S21F4S2F3:"
. "S11F4S22F3S3F3:S12F3S22F3S3F3:S13F3S21F3S4F2:S13F5S20F3S3F3:"
. "S13F6S19F4S2F5:";

my $shark ="S46F9:S41F18:S8F24S5F3S1F21:F41S2F12:F7S2F33S3F9:"
. "S2F22S2F20S2F1S2F4:S4F20S3F22S1F4:S6F2S7F10S1F4S1F3S1F20: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3S5F3S7F1S4F4S3:"
. "S2F3S11F4S3F3S3F4S2F3S4F4S6F2S4F4S3:"
. "S2F4S10F4S3F3S3F4S2F4S3F4S6F2S4F4S3:"
. "S2F6S8F4S3F3S3F4S3F11S4F2S6F4S2:"
. "S2F7S6F6S1F5S1F6S2F6S1F4S2F5S4F6S1:"
. "S2F9S4F6S1F5S1F6S4F2S4F4S1F5S4F6S1:"
. "S2F10S53:S1F2S2F7S53:S1F1S4F7S52:"
. "S1F1S6F5S52:F2S8F3S52:F2S8F3S52:"
. "F2S8F3S52:F2S8F3S52:S1F1S8F3S52:"
. "S1F2S6F3S53:S2F3S3F4S53:S2F9S54:S3F7S55:S65:";

my $newcamel = "newcamel.pl";
my $oldcamel = "camel.pl";
open CAMEL,  "&lt; $oldcamel" or die "camel ($oldcamel) not found\n";
open NEWCAMEL, "&gt; $newcamel" 
    or die "can't create new camel ($newcamel)\n";

my $camel;

while (&lt;CAMEL&gt;) {
	print NEWCAMEL $_,next if /^(:?use|#)/;
	chomp;
	while (/(.)/g) {
		$camel .= $1 if $1 ne " ";
	}
}
close CAMEL;
my $out;
my $choice=shift;
my $pattern = \$llama;

if (defined $choice) {
		$pattern = \$gmax if $choice eq "gmax";
		$pattern = \$shark if $choice eq "shark";
}
else {
	$choice = "llama";
}

my $camelcount =0;
while ($$pattern =~ /([SF:])([^SF:]*)/g) {
	if ($1 eq "F"){
		$out = substr ($camel, $camelcount, $2);
		$camelcount += $2;
	}
	elsif ($1 eq "S"){
		$out = " " x $2;
	}
	else {
		$out = "\n";
	}
	print NEWCAMEL $out;
}
$out = substr($camel,$camelcount);
print NEWCAMEL $out;

close NEWCAMEL;
system "perl $newcamel | "
 . "perl -ne 's/camel/$choice/;print if !/^ ?mm|[\\^]{20}/'";
#do "$newcamel";
&lt;/code&gt;
&lt;font size="-1"&gt;Edit 2001-12-05 by [dvergin] per user request&lt;/link&gt;</field>
</data>
</node>
