.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "Sub::Quote 3pm"
.TH Sub::Quote 3pm "2019-10-13" "perl v5.30.0" "User Contributed Perl Documentation"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Sub::Quote \- Efficient generation of subroutines via string eval
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& package Silly;
\&
\& use Sub::Quote qw(quote_sub unquote_sub quoted_from_sub);
\&
\& quote_sub \*(AqSilly::kitty\*(Aq, q{ print "meow" };
\&
\& quote_sub \*(AqSilly::doggy\*(Aq, q{ print "woof" };
\&
\& my $sound = 0;
\&
\& quote_sub \*(AqSilly::dagron\*(Aq,
\&   q{ print ++$sound % 2 ? \*(Aqburninate\*(Aq : \*(Aqroar\*(Aq },
\&   { \*(Aq$sound\*(Aq => \e$sound };
.Ve
.PP
And elsewhere:
.PP
.Vb 5
\& Silly\->kitty;  # meow
\& Silly\->doggy;  # woof
\& Silly\->dagron; # burninate
\& Silly\->dagron; # roar
\& Silly\->dagron; # burninate
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This package provides performant ways to generate subroutines from strings.
.SH "SUBROUTINES"
.IX Header "SUBROUTINES"
.SS "quote_sub"
.IX Subsection "quote_sub"
.Vb 1
\& my $coderef = quote_sub \*(AqFoo::bar\*(Aq, q{ print $x++ . "\en" }, { \*(Aq$x\*(Aq => \e0 };
.Ve
.PP
Arguments: ?$name, \f(CW$code\fR, ?\e%captures, ?\e%options
.PP
\&\f(CW$name\fR is the subroutine where the coderef will be installed.
.PP
\&\f(CW$code\fR is a string that will be turned into code.
.PP
\&\f(CW\*(C`\e%captures\*(C'\fR is a hashref of variables that will be made available to the
code.  The keys should be the full name of the variable to be made available,
including the sigil.  The values should be references to the values.  The
variables will contain copies of the values.  See the \*(L"\s-1SYNOPSIS\*(R"\s0's
\&\f(CW\*(C`Silly::dagron\*(C'\fR for an example using captures.
.PP
Exported by default.
.PP
\fIoptions\fR
.IX Subsection "options"
.ie n .IP """no_install""" 2
.el .IP "\f(CWno_install\fR" 2
.IX Item "no_install"
\&\fBBoolean\fR.  Set this option to not install the generated coderef into the
passed subroutine name on undefer.
.ie n .IP """no_defer""" 2
.el .IP "\f(CWno_defer\fR" 2
.IX Item "no_defer"
\&\fBBoolean\fR.  Prevents a Sub::Defer wrapper from being generated for the quoted
sub.  If the sub will most likely be called at some point, setting this is a
good idea.  For a sub that will most likely be inlined, it is not recommended.
.ie n .IP """package""" 2
.el .IP "\f(CWpackage\fR" 2
.IX Item "package"
The package that the quoted sub will be evaluated in.  If not specified, the
package from sub calling \f(CW\*(C`quote_sub\*(C'\fR will be used.
.ie n .IP """hints""" 2
.el .IP "\f(CWhints\fR" 2
.IX Item "hints"
The value of \f(CW$^H\fR  to use for the code being evaluated.
This captures the settings of the strict pragma.  If not specified, the value
from the calling code will be used.
.ie n .IP """warning_bits""" 2
.el .IP "\f(CWwarning_bits\fR" 2
.IX Item "warning_bits"
The value of \f(CW\*(C`${^WARNING_BITS}\*(C'\fR  to use for
the code being evaluated.  This captures the warnings set.  If not specified,
the warnings from the calling code will be used.
.ie n .IP """%^H""" 2
.el .IP "\f(CW%^H\fR" 2
.IX Item "%^H"
The value of \f(CW\*(C`%^H\*(C'\fR  to use for the code being evaluated.
This captures additional pragma settings.  If not specified, the value from the
calling code will be used if possible (on perl 5.10+).
.ie n .IP """attributes""" 2
.el .IP "\f(CWattributes\fR" 2
.IX Item "attributes"
The \*(L"Subroutine Attributes\*(R" in perlsub to apply to the sub generated.  Should be
specified as an array reference.  The attributes will be applied to both the
generated sub and the deferred wrapper, if one is used.
.ie n .IP """file""" 2
.el .IP "\f(CWfile\fR" 2
.IX Item "file"
The apparent filename to use for the code being evaluated.
.ie n .IP """line""" 2
.el .IP "\f(CWline\fR" 2
.IX Item "line"
The apparent line number
to use for the code being evaluated.
.SS "unquote_sub"
.IX Subsection "unquote_sub"
.Vb 1
\& my $coderef = unquote_sub $sub;
.Ve
.PP
Forcibly replace subroutine with actual code.
.PP
If \f(CW$sub\fR is not a quoted sub, this is a no-op.
.PP
Exported by default.
.SS "quoted_from_sub"
.IX Subsection "quoted_from_sub"
.Vb 1
\& my $data = quoted_from_sub $sub;
\&
\& my ($name, $code, $captures, $compiled_sub) = @$data;
.Ve
.PP
Returns original arguments to quote_sub, plus the compiled version if this
sub has already been unquoted.
.PP
Note that \f(CW$sub\fR can be either the original quoted version or the compiled
version for convenience.
.PP
Exported by default.
.SS "inlinify"
.IX Subsection "inlinify"
.Vb 4
\& my $prelude = capture_unroll \*(Aq$captures\*(Aq, {
\&   \*(Aq$x\*(Aq => 1,
\&   \*(Aq$y\*(Aq => 2,
\& }, 4;
\&
\& my $inlined_code = inlinify q{
\&   my ($x, $y) = @_;
\&
\&   print $x + $y . "\en";
\& }, \*(Aq$x, $y\*(Aq, $prelude;
.Ve
.PP
Takes a string of code, a string of arguments, a string of code which acts as a
\&\*(L"prelude\*(R", and a \fBBoolean\fR representing whether or not to localize the
arguments.
.SS "quotify"
.IX Subsection "quotify"
.Vb 1
\& my $quoted_value = quotify $value;
.Ve
.PP
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.
.SS "capture_unroll"
.IX Subsection "capture_unroll"
.Vb 4
\& my $prelude = capture_unroll \*(Aq$captures\*(Aq, {
\&   \*(Aq$x\*(Aq => 1,
\&   \*(Aq$y\*(Aq => 2,
\& }, 4;
.Ve
.PP
Arguments: \f(CW$from\fR, \e%captures, \f(CW$indent\fR
.PP
Generates a snippet of code which is suitable to be used as a prelude for
\&\*(L"inlinify\*(R".  \f(CW$from\fR is a string will be used as a hashref in the resulting
code.  The keys of \f(CW%captures\fR are the names of the variables and the values
are ignored.  \f(CW$indent\fR is the number of spaces to indent the result by.
.SS "qsub"
.IX Subsection "qsub"
.Vb 4
\& my $hash = {
\&  coderef => qsub q{ print "hello"; },
\&  other   => 5,
\& };
.Ve
.PP
Arguments: \f(CW$code\fR
.PP
Works exactly like \*(L"quote_sub\*(R", but includes a prototype to only accept a
single parameter.  This makes it easier to include in hash structures or lists.
.PP
Exported by default.
.SS "sanitize_identifier"
.IX Subsection "sanitize_identifier"
.Vb 2
\& my $var_name = \*(Aq$variable_for_\*(Aq . sanitize_identifier(\*(Aq@name\*(Aq);
\& quote_sub qq{ print \e$${var_name} }, { $var_name => \e$value };
.Ve
.PP
Arguments: \f(CW$identifier\fR
.PP
Sanitizes a value so that it can be used in an identifier.
.SH "ENVIRONMENT"
.IX Header "ENVIRONMENT"
.SS "\s-1SUB_QUOTE_DEBUG\s0"
.IX Subsection "SUB_QUOTE_DEBUG"
Causes code to be output to \f(CW\*(C`STDERR\*(C'\fR before being evaled.  Several forms are
supported:
.ie n .IP "1" 4
.el .IP "\f(CW1\fR" 4
.IX Item "1"
All subs will be output.
.ie n .IP """/foo/""" 4
.el .IP "\f(CW/foo/\fR" 4
.IX Item "/foo/"
Subs will be output if their code matches the given regular expression.
.ie n .IP """simple_identifier""" 4
.el .IP "\f(CWsimple_identifier\fR" 4
.IX Item "simple_identifier"
Any sub with the given name will be output.
.ie n .IP """Full::identifier""" 4
.el .IP "\f(CWFull::identifier\fR" 4
.IX Item "Full::identifier"
A sub matching the full name will be output.
.ie n .IP """Package::Name::""" 4
.el .IP "\f(CWPackage::Name::\fR" 4
.IX Item "Package::Name::"
Any sub in the given package (including anonymous subs) will be output.
.SH "CAVEATS"
.IX Header "CAVEATS"
Much of this is just string-based code-generation, and as a result, a few
caveats apply.
.SS "return"
.IX Subsection "return"
Calling \f(CW\*(C`return\*(C'\fR from a quote_sub'ed sub will not likely do what you intend.
Instead of returning from the code you defined in \f(CW\*(C`quote_sub\*(C'\fR, it will return
from the overall function it is composited into.
.PP
So when you pass in:
.PP
.Vb 1
\&   quote_sub q{  return 1 if $condition; $morecode }
.Ve
.PP
It might turn up in the intended context as follows:
.PP
.Vb 1
\&  sub foo {
\&
\&    <important code a>
\&    do {
\&      return 1 if $condition;
\&      $morecode
\&    };
\&    <important code b>
\&
\&  }
.Ve
.PP
Which will obviously return from foo, when all you meant to do was return from
the code context in quote_sub and proceed with running important code b.
.SS "pragmas"
.IX Subsection "pragmas"
\&\f(CW\*(C`Sub::Quote\*(C'\fR 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 \f(CW\*(C`Sub::Quote\*(C'\fR
subs, care should be taken that user pragmas won't effect the rest
of the code.
.SH "SUPPORT"
.IX Header "SUPPORT"
Users' \s-1IRC:\s0 #moose on irc.perl.org
.PP
Development and contribution \s-1IRC:\s0 #web\-simple on irc.perl.org
.PP
Bugtracker: <https://rt.cpan.org/Public/Dist/Display.html?Name=Sub\-Quote>
.PP
Git repository: <git://github.com/moose/Sub\-Quote.git>
.PP
Git browser: <https://github.com/moose/Sub\-Quote>
.SH "AUTHOR"
.IX Header "AUTHOR"
mst \- Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
.SH "CONTRIBUTORS"
.IX Header "CONTRIBUTORS"
frew \- Arthur Axel \*(L"fREW\*(R" Schmidt (cpan:FREW) <frioux@gmail.com>
.PP
ribasushi \- Peter Rabbitson (cpan:RIBASUSHI) <ribasushi@cpan.org>
.PP
Mithaldu \- Christian Walde (cpan:MITHALDU) <walde.christian@googlemail.com>
.PP
tobyink \- Toby Inkster (cpan:TOBYINK) <tobyink@cpan.org>
.PP
haarg \- Graham Knop (cpan:HAARG) <haarg@cpan.org>
.PP
bluefeet \- Aran Deltac (cpan:BLUEFEET) <bluefeet@gmail.com>
.PP
ether \- Karen Etheridge (cpan:ETHER) <ether@cpan.org>
.PP
dolmen \- Olivier Mengué (cpan:DOLMEN) <dolmen@cpan.org>
.PP
alexbio \- Alessandro Ghedini (cpan:ALEXBIO) <alexbio@cpan.org>
.PP
getty \- Torsten Raudssus (cpan:GETTY) <torsten@raudss.us>
.PP
arcanez \- Justin Hunter (cpan:ARCANEZ) <justin.d.hunter@gmail.com>
.PP
kanashiro \- Lucas Kanashiro (cpan:KANASHIRO) <kanashiro.duarte@gmail.com>
.PP
djerius \- Diab Jerius (cpan:DJERIUS) <djerius@cfa.harvard.edu>
.SH "COPYRIGHT"
.IX Header "COPYRIGHT"
Copyright (c) 2010\-2016 the Sub::Quote \*(L"\s-1AUTHOR\*(R"\s0 and \*(L"\s-1CONTRIBUTORS\*(R"\s0
as listed above.
.SH "LICENSE"
.IX Header "LICENSE"
This library is free software and may be distributed under the same terms
as perl itself. See <http://dev.perl.org/licenses/>.
