Perl 5 Internals
Total Page:16
File Type:pdf, Size:1020Kb
Perl 5 Internals Simon Cozens Perl 5 Internals by Simon Cozens Copyright © 2001 by NetThink Open Publications License 1.0 Copyright (c) 2001 by NetThink. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). This series contains material adopted from the Netizen Perl Training Fork (http://spork.sourceforge.net/), by kind permission of Kirrily Robert. Table of Contents 1. Preliminaries ..............................................................................................................1 1.1. Course Outline ..................................................................................................1 1.2. Assumed Knowledge ........................................................................................1 1.3. Objectives..........................................................................................................2 1.4. The course notes ...............................................................................................2 2. Perl Development Structure......................................................................................1 2.1. Perl Versioning..................................................................................................1 2.2. The Development Tracks ..................................................................................1 2.3. Perl 5 Porters.....................................................................................................2 2.4. Pumpkins and Pumpkings.................................................................................2 2.5. The Perl Repository ..........................................................................................3 2.6. Summary...........................................................................................................4 2.7. Exercises ...........................................................................................................4 3. Parts of the Interpreter..............................................................................................6 3.1. Top Level Overview..........................................................................................6 3.2. The Perl Library................................................................................................7 3.3. The XS Library .................................................................................................7 3.4. The IO Subsystem.............................................................................................7 3.5. The Regexp Engine...........................................................................................8 3.6. The Parser and Tokeniser..................................................................................8 3.7. Variable Handling .............................................................................................9 3.8. Run-time Execution ..........................................................................................9 3.9. Support Functions .............................................................................................9 3.10. Testing...........................................................................................................10 3.11. Other Utilities................................................................................................10 3.12. Documentation..............................................................................................10 3.13. Summary.......................................................................................................11 3.14. Exercises .......................................................................................................11 4. Internal Variables ....................................................................................................12 4.1. Basic SVs........................................................................................................12 3 4.1.1. Basics of an SV....................................................................................12 4.1.1.1. sv_any......................................................................................12 4.1.1.2. Reference Counts......................................................................13 4.1.1.3. Flags..........................................................................................14 4.1.2. References............................................................................................15 4.1.3. Integers.................................................................................................16 4.1.4. Strings ..................................................................................................18 4.1.5. Floating point numbers ........................................................................23 4.2. Arrays and Hashes ..........................................................................................23 4.2.1. Arrays...................................................................................................24 4.2.2. Hashes..................................................................................................27 4.2.2.1. What is a "hash" anyway?.........................................................27 4.2.2.2. Hash Entries..............................................................................29 4.2.2.3. Hash arrays................................................................................30 4.3. More Complex Types......................................................................................32 4.3.1. Objects .................................................................................................32 4.3.2. Magic ...................................................................................................34 4.3.3. Tied Variables ......................................................................................38 4.3.4. Globs and Stashes ................................................................................40 4.3.5. Code Values .........................................................................................42 4.3.6. Lexical Variables..................................................................................44 4.4. Inheritance.......................................................................................................46 4.5. Summary.........................................................................................................46 4.6. Exercises .........................................................................................................47 5. The Lexer and the Parser........................................................................................48 5.1. The Parser .......................................................................................................48 5.1.1. BNF and Parsing..................................................................................48 5.1.2. Parse actions and token values.............................................................50 5.1.3. Parsing some Perl.................................................................................51 5.2. The Tokeniser..................................................................................................53 5.2.1. Basic tokenising...................................................................................53 5.2.1.1. Tokeniser State..........................................................................54 5.2.1.2. Looking ahead...........................................................................54 4 5.2.1.3. Keywords ..................................................................................55 5.2.2. Sublexing .............................................................................................55 5.3. Summary.........................................................................................................56 5.4. Exercises .........................................................................................................57 6. Fundamental Operations ........................................................................................59 6.1. The basic op ....................................................................................................59 6.1.1. The different operations.......................................................................60 6.1.2. Different "flavours" of op.....................................................................61 6.1.3. Tying it all together..............................................................................63 6.1.3.1. "Tree" order...............................................................................63 6.1.3.2. Execution Order........................................................................64 6.2. PP Code...........................................................................................................67 6.2.1. The argument stack ..............................................................................67 6.2.2. Stack manipulation...............................................................................68 6.3. The opcode table and opcodes.pl................................................................71 6.4. Scatchpads and Targets ...................................................................................72 6.5. The Optimizer .................................................................................................73