.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" 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 .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Sub::Quote 3pm" .TH Sub::Quote 3pm "2018-12-09" "perl v5.24.1" "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. Numbers aren't treated specially and will be quoted as strings, but undef will quoted as \&\f(CW\*(C`undef()\*(C'\fR. .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 "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 { \& \& \& do { \& return 1 if $condition; \& $morecode \& }; \& \& \& } .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: .PP Git repository: .PP Git browser: .SH "AUTHOR" .IX Header "AUTHOR" mst \- Matt S. Trout (cpan:MSTROUT) .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" frew \- Arthur Axel \*(L"fREW\*(R" Schmidt (cpan:FREW) .PP ribasushi \- Peter Rabbitson (cpan:RIBASUSHI) .PP Mithaldu \- Christian Walde (cpan:MITHALDU) .PP tobyink \- Toby Inkster (cpan:TOBYINK) .PP haarg \- Graham Knop (cpan:HAARG) .PP bluefeet \- Aran Deltac (cpan:BLUEFEET) .PP ether \- Karen Etheridge (cpan:ETHER) .PP dolmen \- Olivier Mengué (cpan:DOLMEN) .PP alexbio \- Alessandro Ghedini (cpan:ALEXBIO) .PP getty \- Torsten Raudssus (cpan:GETTY) .PP arcanez \- Justin Hunter (cpan:ARCANEZ) .PP kanashiro \- Lucas Kanashiro (cpan:KANASHIRO) .PP djerius \- Diab Jerius (cpan:DJERIUS) .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 .