Eval::Util - Utilities related to eval()
This document describes version 0.002 of Eval::Util (from Perl distribution Eval-Util), released on 2018-03-12.
use Eval::Util qw( inside_eval inside_block_eval inside_string_eval eval_level ); # will not print 'foo', but print 'bar' and 'baz' say "foo" if inside_eval(); eval { say "bar" if inside_eval() }; eval q(say "baz" if inside_eval()); # will not print 'foo' or 'baz' but print 'bar' say "foo" if inside_block_eval(); eval { say "bar" if inside_block_eval() }; eval q(say "baz" if inside_block_eval()); # will not print 'foo' or 'bar' but print 'baz' say "foo" if inside_string_eval(); eval { say "bar" if inside_string_eval() }; eval q(say "baz" if inside_string_eval()); say eval_level(); # 0 eval { say eval_level() }; # 1 eval { eval { say eval_level() } }; # 2
None exported by default, but they are exportable.
Usage: inside_eval() => bool
Will check if running code is inside eval() (either string eval or block eval). This is done via examining the stack trace and checking for frame with subroutine named (eval).
(eval)
A faster and simpler alternative is to check if the Perl special variable $^S is true. Consult perlvar for more details about this variable.
$^S
Usage: inside_block_eval() => bool
Will check if running code is inside block eval() (eval { ... }). Will return false if code is only inside string eval. This is done via examining the stack trace and checking for frame with subroutine named (eval) that has undefined eval text.
eval { ... }
Usage: inside_string_eval() => bool
Will check if running code is inside string eval() (eval " ... "). Will return false if code is only inside block eval. This is done via examining the stack trace and checking for frame with subroutine named (eval) that has defined eval text.
eval " ... "
Usage: eval_level() => int
Return 0 if running code is not inside any eval, 1 if inside one eval, 2 if inside two evals, and so on.
Please visit the project's homepage at https://metacpan.org/release/Eval-Util.
Source repository is at https://github.com/perlancar/perl-Eval-Util.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Eval-Util
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
caller in perlfunc
caller
$^S in perlvar
perlancar <perlancar@cpan.org>
This software is copyright (c) 2018 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Eval::Util, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Eval::Util
CPAN shell
perl -MCPAN -e shell install Eval::Util
For more information on module installation, please visit the detailed CPAN module installation guide.