Sub::Quote β Efficient generation of subroutines via string eval
| Use Case | Command | Description |
|---|---|---|
| π¦ Quote a sub | quote_sub 'Name', q{ code }, { captures } | Generate and install a coderef from a string |
| π Force unquote | unquote_sub $sub | Replace a deferred sub with actual compiled code |
| π Inspect quoted sub | quoted_from_sub $sub | Get original quote_sub arguments and compiled version |
| β‘ Inline code | inlinify $code, $args, $prelude | Turn code into an inlineable string with prelude |
| π Quote a value | quotify $value | Quote a scalar for use in code strings |
| π¦ Capture unroll | capture_unroll '$from', \%captures, $indent | Generate prelude code for captures |
| π Quick sub in hash | qsub q{ code } | Like quote_sub but with prototype for single arg |
| π§Ό Sanitize identifier | sanitize_identifier $name | Make a value safe for use in an identifier |
package Silly;
use Sub::Quote qw(quote_sub unquote_sub quoted_from_sub);
quote_sub 'Silly::kitty', q{ print "meow" };
quote_sub 'Silly::doggy', q{ print "woof" };
my $sound = 0;
quote_sub 'Silly::dagron',
q{ print ++$sound % 2 ? 'burninate' : 'roar' },
{ '$sound' => \$sound };
And elsewhere:
Silly->kitty; # meow
Silly->doggy; # woof
Silly->dagron; # burninate
Silly->dagron; # roar
Silly->dagron; # burninate
This package provides performant ways to generate subroutines from strings.
my $coderef = quote_sub 'Foo::bar', q{ print $x++ . "\n" }, { '$x' => \0 };
Arguments: ?$name, $code, ?\%captures, ?\%options
Silly::dagron example.Exported by default.
$^H to use for the code being evaluated. Captures strict pragma settings. Defaults to the caller's value.${^WARNING_BITS} to use. Captures warnings settings. Defaults to the caller's warnings.%^H to use for additional pragma settings. Defaults to the caller's value if possible (perl 5.10+).my $coderef = unquote_sub $sub;
Forcibly replace subroutine with actual code. If $sub is not a quoted sub, this is a no-op. Exported by default.
my $data = quoted_from_sub $sub;
my ($name, $code, $captures, $compiled_sub) = @$data;
Returns original arguments to quote_sub, plus the compiled version if already unquoted. $sub can be either the original quoted version or the compiled version for convenience. Exported by default.
my $prelude = capture_unroll '$captures', {
'$x' => 1,
'$y' => 2,
}, 4;
my $inlined_code = inlinify q{
my ($x, $y) = @_;
print $x + $y . "\n";
}, '$x, $y', $prelude;
Takes a string of code, a string of arguments, a string of code which acts as a "prelude", and a Boolean representing whether or not to localize the arguments.
my $quoted_value = quotify $value;
Quotes a single (non-reference) scalar value for use in a code string. The result should reproduce the original value, including strings, undef, integers, and floating point numbers. The resulting floating point numbers (including infinites and not a number) should be precisely equal to the original, if possible. The exact format of the resulting number should not be relied on, as it may include hex floats or math expressions.
my $prelude = capture_unroll '$captures', {
'$x' => 1,
'$y' => 2,
}, 4;
Arguments: $from, \%captures, $indent
Generates a snippet of code suitable for use as a prelude for inlinify. $from is a string used as a hashref in the resulting code. The keys of %captures are the variable names and the values are ignored. $indent is the number of spaces to indent the result.
my $hash = {
coderef => qsub q{ print "hello"; },
other => 5,
};
Arguments: $code
Works exactly like quote_sub, but includes a prototype to only accept a single parameter. This makes it easier to include in hash structures or lists. Exported by default.
my $var_name = '$variable_for_' . sanitize_identifier('@name');
quote_sub qq{ print \$${var_name} }, { $var_name => \$value };
Arguments: $identifier
Sanitizes a value so that it can be used in an identifier.
Causes code to be output to STDERR before being evaled. Several forms are supported:
1 β All subs will be output/foo/ β Subs whose code matches the given regex will be outputsimple_identifier β Any sub with the given name will be outputFull::identifier β A sub matching the full name will be outputPackage::Name:: β Any sub in the given package (including anonymous subs) will be outputMuch of this is just string-based code-generation, and as a result, a few caveats apply.
Calling return from a quote_sub'ed sub will not likely do what you intend. Instead of returning from the code you defined in quote_sub, it will return from the overall function it is composited into.
So when you pass in:
quote_sub q{ return 1 if $condition; $morecode }
It might turn up in the intended context as follows:
sub foo {
<important code a>
do {
return 1 if $condition;
$morecode
};
<important code b>
}
Which will obviously return from foo, when all you meant was to return from the code context in quote_sub and proceed with running important code b.
Sub::Quote preserves the environment of the code creating the quoted subs. This includes the package, strict, warnings, and any other lexical pragmas. This is done by prefixing the code with a block that sets up a matching environment. When inlining Sub::Quote subs, care should be taken that user pragmas won't effect the rest of the code.
mst β Matt S. Trout (cpan:MSTROUT) <mst AT shadowcat.uk>
Copyright (c) 2010-2016 the Sub::Quote "AUTHOR" and "CONTRIBUTORS" as listed above.
This library is free software and may be distributed under the same terms as perl itself. See http://dev.perl.org/licenses/.
Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-13 00:26 @216.73.217.60
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)