NAME

Eval::Util - Utilities related to eval()

VERSION

This document describes version 0.002 of Eval::Util (from Perl distribution Eval-Util), released on 2018-03-12.

SYNOPSIS

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

DESCRIPTION

FUNCTIONS

None exported by default, but they are exportable.

inside_eval

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).

A faster and simpler alternative is to check if the Perl special variable $^S is true. Consult perlvar for more details about this variable.

inside_block_eval

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.

inside_string_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_level

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.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Eval-Util.

SOURCE

Source repository is at https://github.com/perlancar/perl-Eval-Util.

BUGS

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.

SEE ALSO

caller in perlfunc

$^S in perlvar

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

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.