For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > 'print q^ndzk+^^q?$%*#!?;'









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author 'print q^ndzk+^^q?$%*#!?;'
yusuf

2006-03-28, 6:57 pm

can someone please interpret the print line for me. it prints "JAPH".
Thanks:

perl -e 'print q^ndzk+^^q?$%*#!?;'

Paul Lalli

2006-03-28, 6:57 pm

yusuf wrote:
> can someone please interpret the print line for me. it prints "JAPH".
> Thanks:
>
> perl -e 'print q^ndzk+^^q?$%*#!?;'


This is a very obfuscated program, intentionally so. For starters,
it's making use of the fact that any non-alpha-numeric character can be
used as a delimiter for a string when using the q// operator. The
first one is using the ^ character, and then the second string is using
the ? character. So we can simplify this first to:

print 'ndzk+' ^ '$%*#!';

So we have two strings being used as arguments to the ^ operator. From
there, it's a relatively simply reading of the documentation for that
operator, which can be found in
perldoc perlop:

Binary "^" returns its operators XORed together bit by bit.
(See also Integer Arithmetic and Bitwise String Operators)

Scrolling down farther to the named sections...

Bitwise String Operators

Bitstrings of any size may be manipulated by the bitwise
operators ("~ | & ^").

If the operands to a binary bitwise op are strings of
different sizes, | and ^ ops act as though the shorter
operand had additional zero bits on the right,

So the operation is taking each character of each string, and doing a
bit-wise XOR on those two characters, producing the result:

n ^ $ ==> 0x6E ^ 0x24 ==> 01101110 ^ 00100100 ==> 01001010 ==> 0x4A ==>
J

I hope this helps clarify for you...

Paul Lalli

DJ Stunks

2006-03-28, 6:57 pm


Paul Lalli wrote:
> yusuf wrote:
>
> This is a very obfuscated program, intentionally so. For starters,
> it's making use of the fact that any non-alpha-numeric character can be
> used as a delimiter for a string when using the q// operator. The
> first one is using the ^ character, and then the second string is using
> the ? character. So we can simplify this first to:
>
> print 'ndzk+' ^ '$%*#!';
>
> So we have two strings being used as arguments to the ^ operator. From
> there, it's a relatively simply reading of the documentation for that
> operator, which can be found in
> perldoc perlop:
>
> Binary "^" returns its operators XORed together bit by bit.
> (See also Integer Arithmetic and Bitwise String Operators)
>
> Scrolling down farther to the named sections...
>
> Bitwise String Operators
>
> Bitstrings of any size may be manipulated by the bitwise
> operators ("~ | & ^").
>
> If the operands to a binary bitwise op are strings of
> different sizes, | and ^ ops act as though the shorter
> operand had additional zero bits on the right,
>
> So the operation is taking each character of each string, and doing a
> bit-wise XOR on those two characters, producing the result:
>
> n ^ $ ==> 0x6E ^ 0x24 ==> 01101110 ^ 00100100 ==> 01001010 ==> 0x4A ==>
> J
>
> I hope this helps clarify for you...
>
> Paul Lalli


Interesting:

[jpeavy1@localhost ~]$ perl -MO=Deparse -e'print q^ndzk+^^q?$%*#!?;'
print "JAPH\n";

deparse completes the XOR...

-jp

yusuf

2006-03-28, 6:57 pm

Tangent: why is \n not interpreted if not quoted by double quotes ("")?

e.g. (this prints a new line):

print "hello\n"

(this does not):

print q^hello\n^




DJ Stunks wrote:
> Paul Lalli wrote:
>
> Interesting:
>
> [jpeavy1@localhost ~]$ perl -MO=Deparse -e'print q^ndzk+^^q?$%*#!?;'
> print "JAPH\n";
>
> deparse completes the XOR...
>
> -jp


Paul Lalli

2006-03-28, 6:57 pm

DJ Stunks wrote:
> Paul Lalli wrote:
>
> Interesting:
>
> [jpeavy1@localhost ~]$ perl -MO=Deparse -e'print q^ndzk+^^q?$%*#!?;'
> print "JAPH\n";
>
> deparse completes the XOR...


It's not deparse, it's Perl's internal optimizations. You'll see the
same sort of thing if you do
perl -MO=Deparse -e'$x = 4 + 3;'
$x = 7;
-e syntax OK

Perl optimizes what it can before actually executing any code. An
operation of two constants is something it "knows".

Paul Lalli

Paul Lalli

2006-03-28, 6:57 pm

yusuf wrote:
> Tangent: why is \n not interpreted if not quoted by double quotes ("")?


Because that's the entire point of single quotes and double quotes.
Single quotes do not interpolate. Double quotes do. That's their sole
distinction.

http://perldoc.perl.org/perlintro.h...syntax-overview

Paul Lalli

Uri Guttman

2006-03-29, 3:57 am

>>>>> "PL" == Paul Lalli <mritty@gmail.com> writes:

PL> DJ Stunks wrote:[color=darkred]

PL> It's not deparse, it's Perl's internal optimizations. You'll see the
PL> same sort of thing if you do
PL> perl -MO=Deparse -e'$x = 4 + 3;'
PL> $x = 7;
PL> -e syntax OK

PL> Perl optimizes what it can before actually executing any code. An
PL> operation of two constants is something it "knows".

just to complete the concept it is known as constant folding and has
been used by many compilers over many decades as it is an old
optimization technique. why do some operations (possibly multiple times)
at run time when you know nothing can change later (the expression must
have only constants)? instead you can evaluate the expression once at
compile time. probably one of the easiest compiler optimizations to
write as well.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com