'' =~ (
'(?{'
. ( '+]).][' ^ '[/@@){' )
. '"'
. ( '^@@;.|' ^ '?,,^@^' )
. ',$/})'
)
Update:
can someone please tell me how ( '+]).][' ^ '[/@@){' ) translates to print and how ( '^@@;.|' ^ '?,,^@^' ) translates to allen"?
Take a look at the docs for [cpan://B::Deparse]
C:\test>perl -MO=Deparse
-le"''=~('(?{'.('+]).]['^'[/@@){').chr(34).('^@@;.|'^'?,,^@^').',$/})')"
BEGIN { $/ = "\n"; $\ = "\n"; }
'' =~ m[(?{print "allen",$/})];
-e syntax OK
[BrowserUK] has given you the best general tip above. If you're still wondering how that string turns into that program, an editor with syntax highlighting can help. Here's a breakdown if you still need it.
If you add some white space, you'll see the line looks like:
'' =~ (
'(?{'
. ( '+]).][' ^ '[/@@){' )
. '"'
. ( '^@@;.|' ^ '?,,^@^' )
. ',$/})'
)
So it's matching the empty string against some bizarre pattern. The pattern is built up from concatenating five pieces, three of which are constants:
The other two parts are wrapped in parentheses, and are the result of combining two strings with the exclusive-or operator (^, perlop for more info). Since the parts are constant, perl evaluates them at compile time. If you test it, you'll see that the first expression evaluates to print and the second to allen".
So now you see how Deparse got that string. If you haven't seen the ?{...} construct in Perl regular expressions, take a look at perlre. It allows you to execute code inside a regular expression. In this case it executes the code print "allen",$/.
QED. HTH. :-)
Update
If you want to really get a handle on how the exclusive-or operates to generate the string, it's best to look at the bits-n-bytes level. Try running this little snippet to see.
my @s = (
'+', ']', ')', '.', ']', '[',
'[', '/', '@', '@', ')', '{',
);
for (my $i = 0; $i < 6; $i++)
{
my ($l, $r) = ($s[$i], $s[$i+6]);
printf "%s %x ^ %s %x = %s %x\n",
$l, ord $l,
$r, ord $r,
$l ^ $r, ord $l ^ ord $r;
}
[id://149675|Do not rebuke them with harsh words ... but rather lead them gently - with URLs - so that they may learn wisdom.]
would it be correct to say that this is compiled or parsed code?
No, it would be incorrect. It's "plain" old Perl source code.
This code uses a similar technique to that used by the CPAN Acme::EyeDrops module. To illustrate, running this program:
use Acme::EyeDrops qw(sightly);
print sightly({ Regex => 1,
Print => 1,
Compact => 1,
SourceString => "allen\n" });
produces:
''=~('('.'?'.'{'.('['^'+').('['^')').('`'|')').('`'|'.').('['^'/').'"'.('`'|'!').('`'|',').('`'|',').('`'|'%').('`'|'.').('!'^'+').'"'.'}'.')')
which, when run through Deparse, produces:
'' =~ /(?{print"allen\n"})/;
For more fun, Acme::EyeDrops allows you to pour the generated code into various shapes. For example, running:
use Acme::EyeDrops qw(sightly);
print sightly({ Shape => 'rose',
Regex => 1,
Print => 1,
Indent => 1,
SourceString => "allen\n" });
produces:
''=~('('.'?'
.'{'.('['^"\+").(
'['^')' ).+(
"\`"| (')')).( "\`"|
'.'). ('['^ '/').
"\"".( '`'| '!'). ("\`"|
','). ('`'| ',').( ('`')|
'%'). ('`'| '.').( '!'^'+'
).'"'.'}'. ')'); $:='.'^'~';$~=('@')|
'(';$^=')'^'[';$/ ='`'|('.');$,= '('
^'}';$\='`'|'!';$: =')'^'}';$~ ='*'
|('`');$^= '+'^'_'; $/='&' |'@'
;$,='['&'~' ;$\="\,"^ '|';$:=('.')^
'~';$~="\@"| '(';$^=')'^'[';$/='`'|'.';$,=
'('^'}';$\= '`'|'!';$:=')'^"\}"; ($~)=
'*'|'`';$^ ='+'^'_';$/=('&')| '@';$,
='['&'~'; $\= ','^'|';$: ='.'^'~'
;$~='@'|'(' ;$^= ')' ^('[');$/=
'`'|'.';$,='('^'}';$\= '`'|'!';$:=
')'^'}';$~='*'|'`';$^='+'^'_';$/="\&"|
'@';$,='['&'~';$\= ',' ^((
'|'));$:=('.')^
'~';$~= '@'
|((
'('
));
$^=
( ')')^'['; $/=
(( '`'))|'.'; $,=
"\("^ '}';$\='`' |((
'!')); $:=(')')^ '}'
;($~) ="\*"| '`'
;($^) =(( '+'
))^'_' ;($/)= '&'|'@';$,=
'['&'~';$\= ','^'|';$:=('.')^
'~';$~='@'| '(';$^=')'^ '[';$/=('`')|
'.';$,='('^ "\}";$\= ('`')| '!';$:="\)"^
'}';$~='*'| ('`');$^= '+'^'_'; $/='&'|'@'
;$,=('[')& ('~');$\= ','^"\|"; $:=('.')^
('~');$~= '@'|'(';$^ =')'^'[';$/ =('`')|
'.';$,= '('^'}';$\ ='`'|'!';$: ="\)"^
'}';$~ ='*'|'`'; $^='+'^'_'; ($/)
='&'| '@';$,= '[' &'~';$\= ','
^'|' ;($:) =(( '.'))^'~' ;(
$~) ='@' |(( "\("));
$^ =(( ')'
))^(( '['
)) ;$/
=((
'`'
));
perlmonks.org content © perlmonks.org and axl163, BrowserUk, Burak, eyepopslikeamosquito, ikegami, kwaping, VSarkiss
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03