```{index} single: Optree::Generate ``` # Optree::Generate ```{pperl-module-badges} Optree::Generate ``` Perl's op constructors, for keyword and infix hooks written in Perl: `newBINOP`, `newUNOP`, `newSVOP`, `newLISTOP` and the others build optree fragments, as B::OP objects, from opcodes (`opcode("add")`, or an `OP_ADD` constant imported by name) and other fragments. They work while a sub is being compiled, into its pad; called at any other time they croak. This is a native implementation of XS-Parse-Keyword-FromPerl 0.10's `Generate.xs`; its `Optree/Generate.pm` is used unchanged. ## Synopsis ```perl use Optree::Generate qw(newBINOP newSVOP OP_MULTIPLY OP_CONST); # in a keyword's build hook: the expression it parsed, times two $$outref = newBINOP(OP_MULTIPLY, 0, $args->[0]->op, newSVOP(OP_CONST, 0, 2)); ``` ## Functions ### Opcodes #### `opcode` `opcode($opname)`: the number of the op named `$opname`, lowercase and without `OP_` (`opcode("const")`); an unknown name croaks (`Unrecognised opcode("...")`). Importing `OP_CONST` and the like makes constants of these. ### Constructors #### `op_contextualize` `op_contextualize($op, $context)`: `$op` in context `G_SCALAR`, `G_LIST` or `G_VOID`. #### `op_scope` `op_scope($op)`: `$op` wrapped in a scope of its own, as a block is. #### `newOP` `newOP($type, $flags)`: an op of no operands. #### `newASSIGNOP` `newASSIGNOP($flags, $left, $optype, $right)`: an assignment, `=` with `$optype` 0, or an operator's assignment form (`+=` with `OP_ADD`). #### `newBINOP` `newBINOP($type, $flags, $first, $last)`: an op of two operands. #### `newCONDOP` `newCONDOP($flags, $first, $trueop, $falseop)`: `$first ? $trueop : $falseop`. #### `newFOROP` `newFOROP($flags, $sv, $expr, $block, $cont)`: a `foreach` loop over `$expr`, its variable `$sv` (undef for `$_`) and its `continue` block `$cont` (undef for none). #### `newGVOP` `newGVOP($type, $flags, \*glob)`: an op on a glob; anything other than a glob reference croaks. #### `newLISTOP` `newLISTOP($type, $flags, @kids)`: an op over a list of operands; `OP_LIST` itself is the bare list. #### `newLOGOP` `newLOGOP($type, $flags, $first, $other)`: a short-circuiting op, `OP_AND`, `OP_OR` or `OP_DOR`. #### `newPADxVOP` `newPADxVOP($type, $flags, $padoffset)`: a lexical variable of the sub being compiled, by its pad offset (`OP_PADSV`, `OP_PADAV`, `OP_PADHV`). #### `newSVOP` `newSVOP($type, $flags, $value)`: an op holding a copy of `$value`, a constant with `OP_CONST`. #### `newUNOP` `newUNOP($type, $flags, $first)`: an op of one operand.