Tharg has asked for the wisdom of the Perl Monks concerning the following question:
I hope that makes sense. I'm right on the edge of my skill level here. Here is some code. I've been trying all kinds of combinations in the share_from, share, etc. I'm sure they're wrong, for a start, but I haven't figured out the right way.(in cleanup) Can't locate object method "new" via package "Barcode::Da +taMatrix" (perhaps you forgot to load "Barcode::DataMatrix"?)
Edit : For any future googlers : I went with Safe::Hole in the end and it seems to be doing what I need.use strict; use warnings; use Barcode::DataMatrix; use Data::Dumper; use Safe; sub DataMatrix2d{ my( $value ) = @_; my $mat = Barcode::DataMatrix->new()->barcode( $value ); return $mat; } my @safeops = ( qw( null stub scalar const padany lineseq leaveeval rv2gv ), + # Basic variable IO and traversal qw( lt i_lt gt i_gt le i_le ge i_ge eq i_eq ne i_ne ncmp i_ncmp sl +t sgt sle sge seq sne scmp ), # Comparators qw( preinc i_preinc predec i_predec postinc i_postinc postdec i_po +stdec int hex oct abs pow multiply i_multiply divide i_divide modulo +i_modulo add i_add subtract i_subtract ), # Base math qw( left_shift right_shift bit_and bit_xor bit_or negate i_negate +not complement ), # Binary math qw( match split qr ), # Regex qw( cond_expr flip flop andassign orassign and or xor ), + # Conditionals qw( atan2 sin cos exp log sqrt rand srand ), + # Advanced math qw( rv2cv entersub leavesub pushmark list rv2sv anoncode refgen rv +2av aassign rv2hv helem sassign exists concat subst length return ent +er leave ) # XXX ); my($safefuncs) = new Safe; $safefuncs->permit_only( @safeops ); $safefuncs->share( 'Barcode::DataMatrix' ); $safefuncs->share_from('main', [ 'DataMatrix2d' ]); my $functiontext = 'sub{my($R)=@_;$R->{AA}=DataMatrix2d("001")}'; my $func = $safefuncs->reval( $functiontext ); if( ref( $func ) ne 'CODE' ){ print "Not a code ref"; exit; } my $data = {}; my $ret = $func->( $data ); if( ref( $ret ) ne 'ARRAY' ){ print "Failed"; exit; } printf( "%s\n", Dumper( $ret ) );
|
|---|