Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I pass parameters to Perl subroutines defined using eval?
    primarykey
    data
    text
    <p>I'm using a config file (in YAML) to define types that are used later on to validate other config values required for my app:</p> <pre><code>--- action: &gt; use List::MoreUtils; my $value = $_; any { $value eq $_ } qw(fatal keep merge non-fatal replace); dir : return defined $_ ? -d $_ : -1; file : return defined $_ ? -f $_ : -1; string: 1; --- config-element: value: foo type : file etc ... </code></pre> <p>The idea is to <code>eval</code> each type definition, throw them into a hash and then call to validate configuration data (the following is schematic for easy comprehensibility):</p> <pre><code>#throw sub refs into hash my %type_sub; foreach my $key (keys %$type_def_ref) { my $sub_str = "sub {$type_def_ref-&gt;{$key}}"; $type_sub{$key} = eval $sub_str; } #validate (myfile is a real file in the cwd) print $type_sub{file}-&gt;('myfile'),"\n"; print $type_sub{action}-&gt;('fatal'), "\n"; </code></pre> <p>The problem is that the subroutines in %type_sub don't seem to accept parameters. In the above case, the first print statement outputs <code>-1</code> while the second outputs: </p> <pre><code>Use of uninitialized value $value in string eq at (eval 15) line 1. Use of uninitialized value $_ in string eq at (eval 15) line 1. Can't call method "any" without a package or object reference at (eval 15) line 1. </code></pre> <p>which is not at all what I expect, yet the subroutines are being called.</p> <p>What am I doing wrong? </p> <p>EDIT: I was being sloppy and everything works fine now. Thanks to Friedo.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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