I believe your best bet is to use string eval instead of do and injecting
"no strict qw/vars/;" into the code (to prevent your use strict from
requiring the variables be declared).  I would spend a bit of time trying
to understand where these files come from and fixing that though.  There
has to be a better way of transmitting the data.

#!/usr/bin/perl

use strict;
use JSON;
use warnings;

my $perl_file = shift;
my $perl_code = "no strict qw/vars/;" . do {
local $/;
open my $fh, "<", $perl_file or die "could not open $perl_file: $!";
<$fh>;
};

my $result = eval $perl_code or do {
print "The perl code:\n\n$perl_code\n\ndied with $@";
exit;
};

print encode_json $return;


On Wed, Aug 2, 2017 at 2:55 PM Dyer, Dustin E. (KSC-VAH10) <
dustin.e.d...@nasa.gov> wrote:

> How can I detect and/or error on undefined barewords in perl subroutines?
>
>
>
> I receive data files in perl, and I’d like to read them into Matlab.  Some
> of the files contain barewords that I don’t have an implementation for.
> When I process these files using the *do* command, the undefined
> barewords are silently treated as 0.  This is undesirable because I’d have
> to look through all files provided to me, detect undefined barewords. And
> if I miss one, my data is invalid.
>
>
>
> From what I can tell, Matlab’s perl command returns a character array
> that’s printed to the terminal in perl.  So my process is: run the provided
> file with do, encode the result as json, print the result for use in
> Matlab, and convert the json string into a Matlab struct.
>
>
>
> So is there something different I can do to cause an error when provided
> files contain undefined barewords?
>
>
>
> #start Received file====================
>
> $someVar = [map {$_ - 250 * FT2M} 1,2,3,];  #FT2M is undef
>
> {
>
>   variable => "SomethingCool",
>
>   data => $ someVar
>
> }
>
>
>
> #end Received file=====================
>
>
>
> #start my script ====================
>
> use strict;
>
> use warnings;
>
> use JSON;
>
>
>
> my $json = JSON->new;
>
> my $return = do $ARGV[0];  #using ARGV to get file from user
>
> print encode_json $return;
>
>
>
> #end my script =====================
>
>
>
> Thanks,
>
> Dustin
>
>
>

Reply via email to