Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: What I Most Recently Learned in Perl, But Should Have Already Known

by imp (Priest)
on Aug 21, 2006 at 02:37 UTC ( #568459=note: print w/ replies, xml ) Need Help??


in reply to What I Most Recently Learned in Perl, But Should Have Already Known

I recently learned how to find out what Perl thinks I mean by my code. The magic of B::Deparse. This answered some mysteries, for example why this code will not stop on values that are seen as false:

while (<>) { }
When writing that loop explicitly I would be testing it like this:
while (defined( my $line = <>)) { }
It turns out that is exactly what perl is doing as well, as seen below:
perl -MO=Deparse -e 'while (<>) {}' while (defined($_ = <ARGV>)) { (); }
It's also interesting to see what the parser thinks about other code, such as:
perl -MO=Deparse -e ' if(1) {print "true"}' do { print 'true' };
And
perl -MO=Deparse -e ' print "true" if 1' print 'true';
And it is useful for seeing what some of the code from the Obfuscation section is actually doing.


Comment on Re: What I Most Recently Learned in Perl, But Should Have Already Known
Select or Download Code
Re^2: What I Most Recently Learned in Perl, But Should Have Already Known
by Hue-Bond (Priest) on Aug 21, 2006 at 10:55 UTC
    perl -MO=Deparse -e ' print "true" if 1'  

    If we do if 0 something even funnier happens:

    $ /usr/bin/perl -MO=Deparse -e 'print "true" if 0' '???';

    But which is most amusing is the fact that the deparsed code generates a warning:

    $ /usr/bin/perl -w -e 'print "true" if 0' $ /usr/bin/perl -w -e '"???"' Useless use of a constant in void context at -e line 1.

    --
    David Serrano

      '???' actually is no real thing. It's more like Deparse saying, this should be optimized away.

      E. g. every "Useless use of a constant in void context" also results in this.

Re^2: What I Most Recently Learned in Perl, But Should Have Already Known
by liverpole (Monsignor) on Aug 21, 2006 at 11:25 UTC
    Hi imp,

    I myself didn't know about B::Deparse until about a half a year ago, and only then as a result of being pointed to it by the other monks.

    If you haven't seen it yet, check out this obfuscation, which I wrote to take advantage of some of the properties of B::Deparse.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re^2: What I Most Recently Learned in Perl, But Should Have Already Known
by Freddo (Acolyte) on Mar 17, 2007 at 10:33 UTC
    i have those in my ~/.aliases
    ...
    # Perl
    alias p='perl -nle'                    #> process file by line, chomp
    alias pp='perl -ple'                   #> process file by line, chomp and print
    alias pdbg='perl -de 1'                #> perl debugger
    alias dpl='perl -MO=Deparse'           #> show how perl see this code
    alias dpl2='perl -MO=Deparse,-l,-p,-q,-sCT' #> show how perl see this code, with line no, parens and more
    alias cpan='perl -MCPAN -e shell'      #> Start CPAN
    ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://568459]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2015-07-04 05:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls