perldoc > Sub::Quote

πŸ“› NAME

Sub::Quote β€” Efficient generation of subroutines via string eval

πŸš€ Quick Reference

Use CaseCommandDescription
πŸ“¦ Quote a subquote_sub 'Name', q{ code }, { captures }Generate and install a coderef from a string
πŸ”“ Force unquoteunquote_sub $subReplace a deferred sub with actual compiled code
πŸ” Inspect quoted subquoted_from_sub $subGet original quote_sub arguments and compiled version
⚑ Inline codeinlinify $code, $args, $preludeTurn code into an inlineable string with prelude
πŸ’Ž Quote a valuequotify $valueQuote a scalar for use in code strings
πŸ“¦ Capture unrollcapture_unroll '$from', \%captures, $indentGenerate prelude code for captures
πŸ”— Quick sub in hashqsub q{ code }Like quote_sub but with prototype for single arg
🧼 Sanitize identifiersanitize_identifier $nameMake a value safe for use in an identifier

πŸ“‹ SYNOPSIS

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

πŸ“– DESCRIPTION

This package provides performant ways to generate subroutines from strings.

πŸ› οΈ SUBROUTINES

πŸ”§ quote_sub

my $coderef = quote_sub 'Foo::bar', q{ print $x++ . "\n" }, { '$x' => \0 };

Arguments: ?$name, $code, ?\%captures, ?\%options

Exported by default.

βš™οΈ options

πŸ”“ unquote_sub

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.

πŸ” quoted_from_sub

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.

⚑ inlinify

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.

πŸ’Ž quotify

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.

πŸ“¦ capture_unroll

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.

πŸ”— qsub

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.

🧼 sanitize_identifier

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.

🌍 ENVIRONMENT

πŸ”§ SUB_QUOTE_DEBUG

Causes code to be output to STDERR before being evaled. Several forms are supported:

⚠️ CAVEATS

Much of this is just string-based code-generation, and as a result, a few caveats apply.

πŸ”™ return

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.

πŸ“‹ pragmas

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.

🀝 SUPPORT

✍️ AUTHOR

mst β€” Matt S. Trout (cpan:MSTROUT) <mst AT shadowcat.uk>

πŸ‘₯ CONTRIBUTORS

©️ COPYRIGHT

Copyright (c) 2010-2016 the Sub::Quote "AUTHOR" and "CONTRIBUTORS" as listed above.

πŸ“„ LICENSE

This library is free software and may be distributed under the same terms as perl itself. See http://dev.perl.org/licenses/.

Sub::Quote
πŸ“› NAME πŸš€ Quick Reference πŸ“‹ SYNOPSIS πŸ“– DESCRIPTION πŸ› οΈ SUBROUTINES
πŸ”§ quote_sub πŸ”“ unquote_sub πŸ” quoted_from_sub ⚑ inlinify πŸ’Ž quotify πŸ“¦ capture_unroll πŸ”— qsub 🧼 sanitize_identifier
🌍 ENVIRONMENT
πŸ”§ SUB_QUOTE_DEBUG
⚠️ CAVEATS
πŸ”™ return πŸ“‹ pragmas
🀝 SUPPORT ✍️ AUTHOR πŸ‘₯ CONTRIBUTORS ©️ COPYRIGHT πŸ“„ LICENSE

Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-12 22:31 @216.73.217.60
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^