Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

your obfuscation magic!

by Anonymous Monk
on Apr 24, 2012 at 15:47 UTC ( [id://966882]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

So, share your obfuscation magic with us :p For example the well known $string =~ y===c; this gets the length of the value stored in "string" dont forget to explain your magical code.

Replies are listed 'Best First'.
Re: your obfuscation magic!
by tobyink (Canon) on Apr 24, 2012 at 16:40 UTC
    • Whenever you have to do a numeric calculation, make sure you multiply by the return value from your last print.
    • Speaking of print, there's no need to ever pass any arguments to it.
      { local $_="Hello "; local $\="world\n"; print; }
    • !$, looks pretty non-obvious, but unless you've been tinkering with the $, variable, it's equal to 1. 1 is a number that comes up quite frequently, so be sure to use !$, liberally. Another synonym for 1 is !!$$.
    • On that note, why say unless $foo when you can say if!!$$==!$foo.
    • In general, most of the variables in perlvar are pretty obscure.

    I'll add some more later...

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: your obfuscation magic!
by brx (Pilgrim) on Apr 24, 2012 at 17:12 UTC
    Nested loops with glob or (very very dirty) regex.

    First: for (1..12) { for (1..12) { for (1..12) {...}}}
    #!/usr/bin/perl use strict;use warnings; my $num =3; $"=',';print join "\n",glob join "_",("{@{ [1..12] }}")x$num; ___END___ 1_1_1 1_1_2 1_1_3 1_1_4 1_1_5 1_1_6 ... 12_12_7 12_12_8 12_12_9 12_12_10 12_12_11 12_12_12
    same thing (without $num param) than:
    print join "\n", glob "{1,2,3,4}_{1,2,3,4}_{1_2_3_4}";


    Second: for $i (1 .. 12) { for $j ($i+1 .. 12) { for $k ($j+1 .. 12) {...}}}
    #!/usr/bin/perl use strict;no strict 'refs';use warnings;use re 'eval'; my $num =3; $"='.*?';"@{ [1..12] }" =~ /@{[ ('\b(\d+)\b')x $num]}(?{{print ((j +oin "_",map {$$_}1..$num),"\n")}})(?!)/; __END__ 1_2_3 1_2_4 1_2_5 1_2_6 1_2_7 ... 8_11_12 9_10_11 9_10_12 9_11_12 10_11_12
    same thing (without $num param) than:
    "1 2 3 4" =~ /\b(\d+)\b.*?\b(\d+)\b(?{{print "$1_$2\n"}})(?!)/;


    Update: $" ($LIST_SEPARATOR) permits "implicit join" (for an array interpolated in a string)
Re: your obfuscation magic!
by eyepopslikeamosquito (Archbishop) on Apr 25, 2012 at 08:30 UTC

    As argued in The Lighter Side of Perl Culture (Part III): Obfu constraints are the heart of obfu. Some examples from that node:

    • Constrain yourself to a restricted character set. For example, from A++, ${~$/&~$;&$^}++ uses only punctuation chars, while y ccccd x s vvchr oct oct ord uc ave x s vvucve le s vvuc ave x s vvchr oct oct oct ord uc bve x eval uses only alphabetic chars.
    • Constrain the code to the theme of the obfu shape. For example, when tuning Saturn, I changed O^N^E}(@X,1.6) to S|A|T|U}(@R,$N) to better align the code to the Saturnian theme.
    • Find an arbitrary constraint. For example, another A++ from mtve is constrained to being a palindrome: !$A+++!q!+++A$!

      mm these are all interesting.. a liip using something like regex would be nice.. printing "hello world\n" 10 times for example..
        still looking for magic! :D
Re: your obfuscation magic!
by temporal (Pilgrim) on Apr 24, 2012 at 18:54 UTC

    I had no idea you could replace regex delimiters with... whatever! Can't wait to make my one-liners even more annoyingly obfuscated =P

      Beware that some delimiters have special meanings. e.g. m/$foo/i interpolates $foo whereas m'$foo'i does not.

      Regular expressions are not the only delimiters that can be replaced with whatever. qq(), q(), qx() and qw() work similarly. For example, the following assigns a string consisting of a single space character to $foo:

      my $foo = q q q;
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://966882]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found