obfuscation - Strange perl code - looking for explanation -


i know little bit perl, not enough understand next.

reading perldelta 5.18 found next piece of code already disabled in 5.18. not counting this, still want understand how it's works.

here code , in comments understand

%_=(_,"just "); #initialize %_ hash key=>value   _ => 'just another' $_="perl hacker,\n";    #assign $_ variable "perl..." s//_}->{_/e;            # darkness. /e - evauates expression, but... print 

it prints:

just perl hacker, 

i tried, perl -mo=deparse , next

(%_) = ('_', 'just ');   #initializing %_ hash $_ = "perl hacker,\n";           # above s//%{'_';}/e;                    # substitute beginning of $_ - what? print $_;                        # print result japh syntax ok 

what strange (at least me) - running "deparsed" code doesn't gives original result , prints:

1/8perl hacker, 

i happy:

  1. if can explain code, if write helper code, (with additional steps) helps me understand how works - happens.
  2. explain, why deparsed code not prints original result.

what means %{'_';} in deparsed code?

the code executed substitution operator like

my $code = "do { $repl_expr }"; 

so when replacement expression _}->{_, following executed:

do { _}->{_ } 

_ returns string _ (since strict off), that's same as

do { "_" }->{_} 

which same as

"_"->{_} 

what have there hash element dereference, reference symbolic reference (i.e. string rather actual reference). forbidden strict, here's example of symbolic reference @ work:

%h1 = ( id => 123 ); %h2 = ( id => 456 ); print "h$_"->{id}, "\n"    1..2; 

so means

"_"->{_}    # run-time symbol lookup 

is same as

$_{_}       # compile-time symbol lookup 

a similar trick used in one-liners.

perl -nle'$c += $_; end { print $c }' 

can shortened to

perl -nle'$c += $_; }{ print $c' 

because code executed when -n used obtained equivalent to

my $code = "line: while (<>) { $program }"; 

%{'_';} 

is weird way write

%{'_'} 

which hash dereference. again, reference here symbolic reference. it's equivalent to

%_ 

in scalar context, hash current returns value reports information hash's internals (or false value if empty). there's been suggestion change return number of keys instead.


Comments