Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How does Perl do it it's thing?

by bladx (Chaplain)
on Aug 07, 2001 at 04:14 UTC ( [id://102654]=perlquestion: print w/replies, xml ) Need Help??

bladx has asked for the wisdom of the Perl Monks concerning the following question:

I am still very new to Perl, and I was puzzling over how Perl doesn't really need compiling, compared to other languages, such as C or Java. My question is basically:

How does Perl parse code or "compile" it?

I would like to know how to explain it later when I am helping fellow class mates in a programming class this upcoming school year in september.

Thanks!
Andy

Note: Perhaps some of how Perl does this (parsing) is due to some mysterious forces no one knows about :) ... I don't know.

Replies are listed 'Best First'.
Re: How does Perl do it it's thing?
by blakem (Monsignor) on Aug 07, 2001 at 04:59 UTC
Re: How does Perl do it it's thing?
by bikeNomad (Priest) on Aug 07, 2001 at 04:51 UTC
    Perl, just like any other compiler or interpreter, knows how to read your programs and convert them into a more machine-friendly form for execution. It uses a number of techniques to do this, including using a table-driven parser that is built by a program called YACC (or Bison) by reading a high-level description of the Perl syntax. Here's an excerpt from that description that handles OR (||) and AND (&&) operations (from perly.y):

    expr : expr ANDOP expr { $$ = newLOGOP(OP_AND, 0, $1, $3); } | expr OROP expr { $$ = newLOGOP($2, 0, $1, $3); } | argexpr %prec PREC_LOW ;

    This process builds a so-called "parse tree" where each of the operations is represented by a data structure that points to other data structures.

    In a compiler that expected to output to machine code, this would be approximately the same, but it would be followed by a pass that would read the parse tree and output machine code. In Perl, instead of making machine code for execution by a real processor, a somewhat higher-level representation is made that is then interpreted and executed by a little program inside Perl.

Re: How does Perl do it it's thing?
by LunaticLeo (Scribe) on Aug 07, 2001 at 04:31 UTC
    Perl does parse and "compile". The perl "interpreter" is really a "parse"->"compile"->"execute" program. "Interpreter" usually implies a read->execute->read loop on your script code.

    Perl compiles to a execution tree then starts running the execution tree. Typical systems compile to a linear list of assembly instructions. An execution tree is different than a linear list of assembly instructions in that the branching and expression trees are preserved rather than flattened out as is the case with assembly.

    I've heard that there are optimization advantages to preserving the tree structure information. But really I don't know much about perl internals.

    Hope that helps.

      I've learned many programming languages before, during and after studying computer science at university (Basic, Pascal, C, C++, Java, Gopher, HyperTalk blah blah blah).

      Perl is confusing at first because it doesn't fit into any of the boxes that traditional languages fit into, and my preconceptions of what Perl was going to be like (an advanced scripting language like awk or sed) were completely wrong.

      The best thing to do to truly understand how Perl works is to read Programming Perl, 3rd Edition (O'Reilly) from cover to cover. This book is also known as the "Camel", because it has a picture of a camel on the cover.

      The secret name of Perl is the pathelogically eclectic rubbish lister. The more you learn about Perl the more humorous this name becomes.

      Basically Perl uses the fact that these days we have CPU time and memory to burn. It takes all the advantages of both a compiled language and an interpreted language by half-compiling the raw Perl program into a half-baked execution tree, and then immediately after that it interprets that execution tree to make the final program.

      It is this half-compile, half-interpret process that you are finding confusing - but I think it is important to understand this process in all its detail, so that you can get a deeper understanding of how the perl error reporting systems and debuggers works.

Re: How does Perl do it it's thing?
by Ven'Tatsu (Deacon) on Aug 07, 2001 at 05:33 UTC
Re: How does Perl do it it's thing?
by mattr (Curate) on Aug 07, 2001 at 10:34 UTC
    You might like to take a look at Simon Cozen's PDF of his Perl 5 Internals course notes as mentioned here.

    I enjoyed reading it very much. It is easy to get through, not too long, and covers topics like what yacc is, what basic functions are in what files, how the parse tree is set up, how regular expressions are computed, and lots of one-liner experiments you can do to see the structures created internally for variables and data structures.

    You may also be interested in reading thoughts on Larry Wall quotes which cover some of the philosophical points mentioned in more detail in Larry Wall's Onion documents among others.

Re: How does Perl do it it's thing?
by da (Friar) on Aug 07, 2001 at 06:33 UTC
    If you're interested in perl's data structures, there is also PerlGuts Illustrated which probably won't make sense unless you read it along with perlguts, but adds a lot of details that will be useful if you want to explain (and understand) it in depth.

    ___ -DA > perl -MPOSIX -e'$ENV{TZ}="US/Eastern";print ctime(10**9)' Sat Sep 8 21:46:40 2001
Re: How does Perl do it it's thing?
by clemburg (Curate) on Aug 07, 2001 at 17:43 UTC

    There is a very good and readable discussion of this in chapter 20 of Advanced Perl Programming.

    The only way to get much deeper into it starts with fetching a copy of the current perl version:

    rsync -auvz rsync://ftp.linux.activestate.com/perl-current/ /usr/local +/lib/perl/bleadperl/

    Then building a debugging-enabled perl with debugging symbols in it (note the make option, this cost me some hours to find out):

    pushd /usr/local/lib/perl/bleadperl make realclean rm config.sh ./Configure -de -Dcc=gcc -Dusedevel -Doptimize=-g3 make OPTIMIZE=-g3 make test popd

    And then starting up your favorite debugger with a breakpoint at run.c:Perl_runops_debug, e.g., in gdb:

    break run.c:Perl_runops_debug run -e '$foo = 42'

    If that is your idea of fun, of course ...

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: How does Perl do it it's thing?
by Nitsuj (Hermit) on Aug 07, 2001 at 06:44 UTC
    Perl is an interpretter. Interpretters use languages in plaintext or a compiled form by reading instructions that go into another program, rather than compiled machine code, such as C, which is translated into the bytecodes of the processor. JAVA is an interesting situation, in that it is compiled bytecodes for a "virtual machine." Which is pretty much interpretting something that looks akin to machine code.

    There is some validity to the idea that perl is compiled. It preparses the code into trees and and uses other methods which are a strong departure from the traditional idea of an interpretter.

    Just Another Perl Backpacker

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found