World Library  
Flag as Inappropriate
Email this Article

Perl virtual machine

Article Id: WHEBN0028034547
Reproduction Date:

Title: Perl virtual machine  
Author: World Heritage Encyclopedia
Language: English
Subject: Perl, Perl package manager, Qrpff, W3Catalog, Matt's Script Archive
Collection: Perl, Stack-Based Virtual MacHines
Publisher: World Heritage Encyclopedia

Perl virtual machine

The Perl virtual machine is a stack-based process virtual machine implemented as an opcodes interpreter which runs previously compiled programs written in the Perl language. The opcodes interpreter is a part of the Perl interpreter, which also contains a compiler (lexer, parser and optimizer) in one executable file, commonly /usr/bin/perl on various Unix-like systems or perl.exe on Microsoft Windows systems.


  • Implementation 1
    • Opcodes 1.1
    • Variables 1.2
    • Data structures 1.3
      • data types 1.3.1
      • stash 1.3.2
      • padlist 1.3.3
    • Stacks 1.4
      • Argument stack 1.4.1
      • Mark stack 1.4.2
      • Save stack 1.4.3
      • Scope stack 1.4.4
    • Other implementations 1.5
  • See also 2
  • References 3
  • External links 4



The Perl compiler outputs a compiled program into memory as an internal structure which can be represented as a tree graph in which each node represents an opcode. Opcodes are represented internally by typedefs. Each opcode has next / other and first / sibling pointers, so the opcode tree can be drawn as a basic OP tree starting from root node or as flat OP list in the order they would normally execute from start node. Opcodes tree can be mapped to the source code, so it is possible to decompile to high-level source code.[1]

Perl's opcodes interpreter is implemented as a tree walker which travels by opcode tree in execute order from start node, following the next or other pointers. Each opcode has a function pointer to a pp_opname function, i.e. say opcode calls pp_say function of internal Perl API.

The phase of compiling the Perl program is hidden for the end user, but it can be exposed with B Perl module[2] or other specialized modules like B::Concise Perl module.[3]

An example of compiled simple Hello world program with a help of B::Concise Perl module, dumped in execute order:

$ perl -MO=Concise,-exec -E 'say "Hello, world!"'
1  <0> enter
2  <;> nextstate(main 46 -e:1) v:%,{
3  <0> pushmark s
4  <$> const[PV "Hello, world!"] s
5  <@> say vK
6  <@> leave[1 ref] vKP/REFC

Some opcodes (entereval, dofile, require) call Perl compiler functions which generate other opcodes in the same Perl virtual machine.


Perl variables can be global, dynamic (local keyword), or lexical (my and our keywords).

Global variables are accessible via the stash and the corresponding typeglob.

Local variables are the same as global variables but a special opcode is generated to save its value on savestack and restore it later.

Lexical variables are stored on padlist.

Data structures

Perl VM data structures are represented internally by typedefs.

The internal data structures can be examined with B Perl module[2] or other specialized tools like Devel::Peek Perl module.[4]

data types

Perl has three typedefs that handle Perl's three main data types: Scalar Value (SV), Array Value (AV), Hash Value (HV). Perl uses a special typedef for simple signed integer type (IV), an unsigned integer (IV), a floating point number (NV) and string (PV).

Perl uses a reference count-driven garbage collection mechanism. SVs, AVs, or HVs start their life with a reference count of 1. If the reference count of a data value ever drops to 0, then it will be destroyed and its memory made available for reuse.

Other typedefs are Glob Value (GV) which contains named references to the various objects, Code Value (CV) which contains a reference to Perl subroutine, I/O Handler (IO), a reference to regular expression (REGEXP; RV in Perl before 5.11), reference to compiled format for output record (FM) and simple reference which is a special type of scalar that point to other data types (RV).


Special Hash Value is stash, a hash that contains all variables that are defined within a package. Each value in this hash table is a Glob Value (GV).


Special Array Value is padlist which is an array of array. Its 0th element to an AV containing all lexical variable names (with prefix symbols) used within that subroutine. The padlist's first element points to a scratchpad AV, whose elements contain the values corresponding to the lexical variables named in the 0th row. Another elements of padlist are created when the subroutine recurses or new thread is created.


Perl has a number of stacks to store things it is working on.

Argument stack

Arguments are passed to opcode and returned from opcode using the argument stack. The typical way to handle arguments is to pop them off the stack, and then push the result back onto the stack.

Mark stack

This stack saves bookmarks to locations in the argument stack usable by each function so the functions doesn't necessarily get the whole argument stack to itself.

Save stack

This stack is used for saving and restoring a value of dynamically scoped local variable.

Scope stack

This stack stores information about actual scope and it is used only for debugging purposes.

Other implementations

There is no standarization for Perl language and Perl virtual machine. The internal API should be considered as non-stable and changes from version to version. The Perl virtual machine is tied closely to compiler. These things make very hard to reimplement Perl virtual machine.

The most known and most stable implementation is a B::C Perl module[5] which translates opcodes tree to representation in C language and adds own tree walker.

Another implementation is an Acme::Perl::VM Perl module[6] which is an implementation coded in Perl language only but it is still tied with original Perl virtual machine via B:: modules.

See also


  1. ^ "B::Deparse - Perl compiler backend to produce perl code". 
  2. ^ a b "B - The Perl Compiler Backend". 
  3. ^ "B::Concise - Walk Perl syntax tree, printing concise info about ops". 
  4. ^ "Devel::Peek - A data debugging tool for the XS programmer". 
  5. ^ "B::C - Perl compiler's C backend". 
  6. ^ "Acme::Perl::VM - A Perl5 Virtual Machine in Pure Perl (APVM)". 

External links

  • The Perl internals: running stage
  • Introduction to the Perl API
  • The "B" op tree.
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.