Compile-time Manipulation
Explore how to manipulate Perl code during compilation to improve maintainability and flexibility. Understand the use of BEGIN blocks for compile-time execution and how to dynamically create and introspect classes with Class::MOP, avoiding fragile string evals and manual symbol table edits.
Unlike code written explicitly as code, code generated through string eval gets compiled while our program is running. Where we might expect a normal function to be available throughout the lifetime of our program, a generated function might not be available when we expect it.
The BEGIN block
Force Perl to run code—to generate other code—during compilation by wrapping it in a BEGIN block. When the Perl parser encounters a block labeled BEGIN, it parses and compiles the entire block and then runs it (unless it has syntax errors). When the block finishes running, parsing ...