Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
It is no fun to write something you think is clever, unless you can show off with all the nitty-gritty. I was really hoping that someone bored enough would attempt to decode this one, but I decided to post a spoiler myself instead. Hopefully, some people will enjoy the "tricks" I tried to employ - almost all of them are misdirections, trying to make you beleive something else is happening. Did it work? Inquiring minds want to know... anyways, thanks for letting me waste some of your time. Keep on making unmaintainable code out there. :)
#!/usr/bin/perl -wl0040
These switches sets some special variables for me, which I will be using later. It sets $/ to a newline character, and $\ to a space character. See perlrun for details.
use vars qw( $ARG );
Minor thingy that will not fool many, but I don't like being sloppy, can I avoid it; this has no actual effect, but tries to hide the fact that use English; aliases $_ to $ARG.
$ARG = qr(

  /^C^C^C^P\\\[^D"\[^P^C""0"\0\/""\0""!!0""0\0""0\0""0!!\/""
  "0"\[^P^C0\[!^C^P"\[^C0^P\/\\\0"!"0"!"0\["\\"0""0\/^P^C0"
  ""0"\[\[\\\\0"\0/
);
Here is a fun part. This looks very much like a hairy regular expression, yes? Well, I am not gonna use it as a such. Actually, I am using qr instead of q - I am gonna use $ARG as a plain string. I also use the slashes at the start and end (marked with blue) to make it look even more like a regular expression - in reality, they would probably break your otherwise valid regexp if you add them. I will get back to this later, though.

So, what about this match then?

($_=~s/$ARG/JAPH/ ||
  s/(\W)/\$$1/g && s/\$(\s)\$//g && s'([^/]+)'#$1\')
    or warn "No match!";	
When you have figured out that $_ and $ARG are the same, you will expect this to match, and the rest of the line to not execute. Also, if you are a bit gullible, you might suspect that the JAPH part has something to do with the final output. But if you use this "regexp" as a string, it will look like this:
 (?-xism:

  /^C^C^C^P\\\[^D"\[^P^C""0"\0/""\0""!!0""0\0""0\0""0!!/"""
  0"\[^P^C0\[!^C^P"\[^C0^P/\\\0"!"0"!"0\["\\"0""0/^P^C0"""0
  "\[\[\\\\0"\0/
Which will not match the regexp that it forms, so in reality it could just as well say:
(0 || s/(\W)/\$$1/g && s/\$(\s)\$//g
  && s'([^/]+)'#$1\') or warn "No match!";	
($_=~s/$ARG/JAPH/ || s/(\W)/\$$1/g &&
  s/\$(\s)\$//g && s'([^/]+)'#$1\') or warn "No match!";	
This part is straightforward: it simply adds a dollarsign ($) in front of every non-word character, producing:
 $($?$-xism$:$
$       $
$ $ $/$^C$^C$^C$^P$\$\$\$[$^D$"$\$[$^P$^C$"$"0$"$\0$/$"$"$\
0$"$"$!$!0$"$"0$\0$"$"0$\0$"$"0$!$!$/$"$"$"0$"$\$[$^P$^C0$\
$[$!$^C$^P$"$\$[$^C0$^P$/$\$\$\0$"$!$"0$"$!$"0$\$[$"$\$\$"0
$"$"0$/$^P$^C0$"$"$"0$"$\$[$\$[$\$\$\$\0$"$\0$/$
$)
Notice the highlighted part, because next regexp is just there to take that part out, with the newline. No magic. :)
($_=~s/$ARG/JAPH/ || s/(\W)/\$$1/g && s/\$(\s)\$//g &&
  s'([^/]+)'#$1\') or warn "No match!";	
($_=~s/$ARG/JAPH/ || s/(\W)/\$$1/g && print
  && s/\$(\s)\$//g && s'([^/]+)'#$1\') or warn "No match!";	

print qq($') && eval;
The highlighting should explain it - most would expect that I am matching something (actually, everything up till the first forward slash), and then adding a '#' in front of it. And since everything (mostly) seems to close alright, and we even have a warning in case something goes wrong, this should be it. But in reality, I am substituting everything up till said slash with the beginning of the next line too. See the green highlights to see which outer parenthesis actually match too. This makes our $_ look like this at the moment:
 #$1') or warn "No match!";

print qq($/$^C$^C$^C$^P$\$\$\$[$^D$"$\$[$^P$^C$"$"0$"$\0$/$
"$"$\0$"$"$!$!0$"$"0$\0$"$"0$\0$"$"0$!$!$/$"$"$"0$"$\$[$^P$
^C0$\$[$!$^C$^P$"$\$[$^C0$^P$/$\$\$\0$"$!$"0$"$!$"0$\$[$"$\
$\$"0$"$"0$/$^P$^C0$"$"$"0$"$\$[$\$[$\$\$\$\0$"$\0$/)
Which, when evaled, of course interpolates all these variables. Depending on which variables, they have different defaults (they are perl special vars), most of them zero, some are empty (just for show) and like I mentioned in the beginning, I also set $/ to a newline character, and $\ to space. This also made it possible to make \/ look like I was escaping slashes in the "regexp". So in reality, this code looks like this:
print "\n0000   00  000  0  0 \n   0  0  0 0  0 0  0 \n   
0  0000 000  0000 \n   0  0  0 0    0  0 \n000   0  0 0   
 0  0\n";
Which, of course, will print:
0000   00  000  0  0
   0  0  0 0  0 0  0
   0  0000 000  0000
   0  0  0 0    0  0
000   0  0 0    0  0
Please point out if something didn't make sense, was unclear (or somehow wrong), and I'll of course fix it. Any comments also very much appreciated (thanks grinder!).
You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

Edit: chipmunk 2002-02-28


In reply to SOLUTION: Amazing Regexp Grinder (ARG) by Dog and Pony
in thread Amazing Regexp Grinder (ARG) by Dog and Pony

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found