Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In this case, there is <strong>no need</strong> to use the string form of <code>eval</code> (or any form for that matter). Not only is it slow, but it also can silently trap errors and could lead to untrusted code execution if used with a tainted input. To write a virtual function call in Perl, you can either work with the symbol table directly, or use a symbolic reference:</p> <pre><code>use 5.010; use warnings; use strict; {package HashValidator; sub test_ok {exists $_[0]{ok}} sub test_fail {exists $_[0]{fail}} } my $hashref = {ok =&gt; 1}; my $errors; for my $vFunction qw(test_ok test_fail) { # to call the function: say "glob deref: $vFunction: ", $HashValidator::{$vFunction}-&gt;($hashref); {no strict 'refs'; say "symbolic: $vFunction: ", &amp;{"HashValidator::$vFunction"}($hashref)} # to conditionally use the result (if it is a true boolean value): if (my $ret = $HashValidator::{$vFunction}-&gt;($hashref)) { push @$errors, $ret; } # or to keep the function call in list context: push @$errors, grep $_, $HashValidator::{$vFunction}-&gt;($hashref); # or to golf it: push @$errors, $HashValidator::{$vFunction}-&gt;($hashref) || (); } say @$errors.': ', join ', ' =&gt; @$errors; </code></pre> <p>which prints:</p> <pre><code>glob deref: test_ok: 1 symbolic: test_ok: 1 glob deref: test_fail: symbolic: test_fail: 3: 1, 1, 1 </code></pre> <p>If you are working with object oriented code, virtual method calls are even easier, with no symbol table or symbolic references:</p> <pre><code>$obj-&gt;$vMethod(...) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload