Skip to Content
Perl 6 and Parrot Essentials, Second Edition
book

Perl 6 and Parrot Essentials, Second Edition

by Allison Randal, Dan Sugalski, Leopold Tötsch
June 2004
Intermediate to advanced
304 pages
12h 19m
English
O'Reilly Media, Inc.
Content preview from Perl 6 and Parrot Essentials, Second Edition

Loading Bytecode

In addition to running Parrot bytecode on the command line, you can also load precompiled bytecode directly into your PASM source file. The load_bytecode opcode takes a single argument: the name of the bytecode file to load. So, if you create a file named file.pasm containing a single subroutine:

# file.pasm
.pcc_sub _sub2:               # .pcc_sub stores a global sub
   print "in sub2\n"
   invoke P1

and compile it to bytecode using the -o command-line switch:

$ parrot -o file.pbc file.pasm

You can then load the compiled bytecode into main.pasm and directly call the subroutine defined in file.pasm:

# main.pasm
_main:
  load_bytecode "file.pbc"    # compiled file.pasm
  find_global P0, "_sub2"
  invokecc
  end

The load_bytecode opcode also works with source files, as long as Parrot has a compiler registered for that type of file:

# main2.pasm
_main:
  load_bytecode "file.pasm"  # PASM source code
  find_global P0, "_sub2"
  invokecc
  end

Subroutines marked with @LOAD run as soon as they’re loaded (before load_bytecode returns), rather than waiting to be called. A subroutine marked with @MAIN will always run first, no matter what name you give it or where you define it in the file.

# file3.pasm
.pcc_sub @LOAD _entry:        # mark the sub as to be run
  print "file3\n"
  invoke P1                   # return

# main3.pasm
_first:                       # first is never invoked
  print "never\n"
  invoke P1

.pcc_sub @MAIN _main:         # because _main is marked as the
  print "main\n"              # MAIN entry of program execution
  load_bytecode "file3.pasm"
  print "back\n"
  end

This example ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Perl 6 Deep Dive

Perl 6 Deep Dive

Andrew Shitov
Perl 6 Essentials

Perl 6 Essentials

Allison Randal, Dan Sugalski, Leopold Tötsch
Think Perl 6

Think Perl 6

Laurent Rosenfeld, Allen B. Downey
Perl by Example

Perl by Example

Ellie Quigley

Publisher Resources

ISBN: 059600737X