Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Dear monks,
This one was a slow Saturday, and I spent some idle time looking at the 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.
In order to run this program, you must have the Camel code in your current directory. (I only tried it in Linux and FreeBSD.)
perl camelfilter.pl # shows the llama perl camelfilter.pl shark perl camelfilter.pl gmax
The patterns for shark and 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 banner 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.
+------------------+ | | | while pattern |<------------------+ <-----------------+ | | | | +------------------+ | | | | | V | | /\ | | / \ +----------+----------+ | / \ | | | / is it\____NO_______> | write next non space| | \ space/ | characters from | | \ ? / | camel.pl | | \ / +---------------------+ | \/ | | | | | YES | | | V | +---------------------+ | | write as many spaces| | | as stated in the +------------------------------------+ | pattern | +---------------------+
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).
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
system "perl $newcamel | perl -ne 'print if !/^ ?mm|[\\^]{20}/'";
or show them
do "$newcamel";
There is room for improvement (patterns could be bit-encoded, for instance), but I would like some specific hints on how to apply the pattern in a more linear way than my while ($$pattern ...). 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.)
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.
TIAFYH
gmax
#!/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:" . "S11F2S7F14S1F1S1F15S1F3:S13F3S8F11S1F1S1F1S1F18:" . "S14F7S6F6S1F1S2F21:S16F21S1F9S1F13:S19F20S1F3S2F2S1F13:" . "S19F2S4F14S3F6S2F12:S17F7S9F6S1F2S3F4S1F13:S16F5S17F8S2F1S2F13:" . "S16F1S26F21:S53F12:S48F3S5F9:S50F6S2F7:S52F12:S53F10:S54F6:" . "S54F5:S53F4:S53F2:F1:"; my $gmax = "S65:S12F2S51:S11F4S50:S12F2S51:" . "S5F4S3F2S1F5S3F2S4F2S8F3S7F7S3F5S1:" . "S4F6S1F2S2F5S2F3S3F4S5F7S5F7S3F5S1:" . "S3F3S2F3S5F4S1F5S1F5S5F2S2F3S6F5S6F2S2:" . "S2F3S3F4S4F10S1F6S3F3S3F3S6F4S5F2S3:" . "S1F4S4F3S4F5S1F5S2F4S3F3S3F4S5F4S5F1S4:" . "S1F4S4F4S3F4S3F4S2F4S3F3S4F3S6F4S3F2S4:" . "S1F3S6F3S3F4S3F3S3F4S3F3S4F3S6F4S3F2S4:" . "F4S6F3S3F4S3F3S3F4S4F1S5F3S7F4S2F1S5:" . "F4S6F3S3F4S3F3S3F4S10F3S7F4S1F2S5:" . "F4S6F3S3F4S3F3S3F4S10F3S8F5S6:" . "F4S6F3S3F4S3F3S3F4S5F2S3F3S8F5S6:" . "F4S6F3S3F4S3F3S3F4S4F5S1F3S8F5S6:" . "S1F4S4F4S3F4S3F3S3F4S3F10S8F5S6:" . "S1F4S4F4S3F4S3F3S3F4S2F4S2F5S9F4S6:" . "S2F3S4F3S4F4S3F3S3F4S2F3S4F4S9F4S6:" . "S2F3S3F3S5F4S3F3S3F4S2F3S5F3S8F6S5:" . "S3F7S6F4S3F3S3F4S1F4S5F3S8F6S5:" . "S4F6S6F4S3F3S3F4S1F4S5F3S8F1S2F4S4:" . "S4F1S11F4S3F3S3F4S1F4S5F3S7F2S2F4S4:" . "S3F2S11F4S3F3S3F4S2F3S5F3S7F1S4F4S3:" . "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, "< $oldcamel" or die "camel ($oldcamel) not found\n"; open NEWCAMEL, "> $newcamel" or die "can't create new camel ($newcamel)\n"; my $camel; while (<CAMEL>) { 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";
Edit 2001-12-05 by dvergin per user request</link>

In reply to Reusing camel code by gmax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (9)
    As of 2014-07-25 11:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (170 votes), past polls