Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Hacking Perl Code

by carlriz (Beadle)
on Apr 08, 2014 at 20:14 UTC ( #1081552=perlquestion: print w/ replies, xml ) Need Help??
carlriz has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I was reading 'cool uses for perl' and came across some code. What is the '=<<' operator? What is 'eval $begin.$content;' (specifically, you can concat. these?)? Just curious how this works (maybe the rest of the code too).

my $file = $ARGV[0]; my $content = do { open my $fh, '<', $file or die $!; local $/; <$fh> +}; my $begin =<<'THEEND'; UNITCHECK { no strict; # access $VERSION by symbolic reference no warnings qw (uninitialized); print map { s!/!::!g; s!.pm$!!; sprintf "%-20s %s\n", $_, ${"${_}::VERSION"} } sort keys %INC; exit; }; THEEND eval $begin.$content; print $@ if $@;

Comment on Hacking Perl Code
Download Code
Re: Hacking Perl Code
by choroba (Abbot) on Apr 08, 2014 at 20:31 UTC
    There is no =<< operator. It is an assignment operator = and a HERE-document (see perlop for both).

    What's wrong on concatenating two scalar variables, whose contents are strings? Check eval EXPR for the meaning.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Hacking Perl Code
by Anonymous Monk on Apr 08, 2014 at 20:34 UTC

    The my $content = do { ... }; bit is an idiom that reads the entire contents of the file named by $file and stores it in $content.

    << introduces a here doc. Everything between that line and the end marker THEEND is a string that gets stored in $begin.

    $begin.$content concatenates the two strings, and eval executes that code.

      Very interesting idiom...I enjoyed the read.

Re: Hacking Perl Code
by kcott (Abbot) on Apr 08, 2014 at 20:35 UTC

    G'day carlriz,

    That's actually an assignment:

    my $begin =

    With a heredoc:

    <<'THEEND'; ... THEEND

    For more details, see "perlop: Quote-Like Operators". Scroll down to <<EOF for heredoc specifics. You'll find additional, relevant information in the "Gory details of parsing quoted constructs" section, just after that.

    -- Ken

Re: Hacking Perl Code (Deparse ppi_dumper perldoc)
by Anonymous Monk on Apr 08, 2014 at 20:38 UTC

    B::Deparse can help you de-obfuscate code a bit

    ppi_dumper can help you intrepret code without running it (tells you what friendly manual to read )

    $ ppi_dumper carliz-hacking.pl PPI::Document PPI::Token::Whitespace '\n' PPI::Statement::Variable PPI::Token::Word 'my' PPI::Token::Whitespace ' ' PPI::Token::Symbol '$file' PPI::Token::Whitespace ' ' PPI::Token::Operator '=' PPI::Token::Whitespace ' ' PPI::Token::Symbol '$ARGV' PPI::Structure::Subscript [ ... ] PPI::Statement::Expression PPI::Token::Number '0' PPI::Token::Structure ';' PPI::Token::Whitespace '\n' PPI::Statement::Variable PPI::Token::Word 'my' PPI::Token::Whitespace ' ' PPI::Token::Symbol '$content' PPI::Token::Whitespace ' ' PPI::Token::Operator '=' PPI::Token::Whitespace ' ' PPI::Token::Word 'do' PPI::Token::Whitespace ' ' PPI::Structure::Block { ... } PPI::Token::Whitespace ' ' PPI::Statement PPI::Token::Word 'open' PPI::Token::Whitespace ' ' PPI::Token::Word 'my' PPI::Token::Whitespace ' ' PPI::Token::Symbol '$fh' PPI::Token::Operator ',' PPI::Token::Whitespace ' ' PPI::Token::Quote::Single ''<'' PPI::Token::Operator ',' PPI::Token::Whitespace ' ' PPI::Token::Symbol '$file' PPI::Token::Whitespace ' ' PPI::Token::Operator 'or' PPI::Token::Whitespace ' ' PPI::Token::Word 'die' PPI::Token::Whitespace ' ' PPI::Token::Magic '$!' PPI::Token::Structure ';' PPI::Token::Whitespace ' ' PPI::Statement::Variable PPI::Token::Word 'local' PPI::Token::Whitespace ' ' PPI::Token::Magic '$/' PPI::Token::Structure ';' PPI::Token::Whitespace ' ' PPI::Statement PPI::Token::Operator '<' PPI::Token::Symbol '$fh' PPI::Token::Operator '>' PPI::Token::Whitespace ' ' PPI::Token::Structure ';' PPI::Token::Whitespace '\n' PPI::Statement::Variable PPI::Token::Word 'my' PPI::Token::Whitespace ' ' PPI::Token::Symbol '$begin' PPI::Token::Whitespace ' ' PPI::Token::Operator '=' PPI::Token::HereDoc '<<'THEEND'' PPI::Token::Structure ';' PPI::Token::Whitespace '\n' PPI::Token::Whitespace '\n' PPI::Statement PPI::Token::Word 'eval' PPI::Token::Whitespace ' ' PPI::Token::Symbol '$begin' PPI::Token::Operator '.' PPI::Token::Symbol '$content' PPI::Token::Structure ';' PPI::Token::Whitespace '\n' PPI::Statement PPI::Token::Word 'print' PPI::Token::Whitespace ' ' PPI::Token::Magic '$@' PPI::Token::Whitespace ' ' PPI::Token::Word 'if' PPI::Token::Whitespace ' ' PPI::Token::Magic '$@' PPI::Token::Structure ';' PPI::Token::Whitespace '\n' PPI::Token::Whitespace '\n'

    OPerators are documented in perlop, functions in perlfunc, magic variables in perlvar ... its just a matter of looking up each piece you're unsure about... perldoc -f eval

    Tutorials: Perl documentation documentation, Searching Perl Documentation, How to Read The Fine Friendly Manual

      That's pretty useful. Thanks

Re: Hacking Perl Code
by LanX (Canon) on Apr 08, 2014 at 21:03 UTC
    You should have better asked here -> "check modules used by a script and their version" or at least link to the original thread.

    What it does is to compile a file's content given as parameter within an eval after prepending a UNITCHECK block which is executed right after compilation and exits right away.

    Within this block the required (resp used) modules are parsed from the keys of %INC.

    But the author is overly optimistic about parsing code without executing it. It was already mentioned that BEGIN blocks are executed at compile time, additionally does every use execute an import function.

    And much more magic ...

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      thanks LanX, as suggested I added a link to this post in the original one.

      I also added an update to the same post to explain better my optimistic sentence parsing code without executing it. Doing this i discovered that my solution executes even less code than the perl -c -d:TraceUse script.pl one.

      HtH
      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        > Doing this i discovered that my solution executes even less code than the perl -c -d:TraceUse script.pl one.

        Could you please elaborate what you mean with less code?

        Can you provide an example showing the difference?

        TraceUse hooks into @INC with a callback like recently discussed.

        With this approach its even possible to report before anything is included and to avoid the import.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-09-24 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (245 votes), past polls