I want a function which uses Barcode::DataMatrix in a safe container. So then I can "safe"ly reval code passed as a sting, which might contain this function which uses Barcode::DataMatrix. Currently I always get :
(in cleanup) Can't locate object method "new" via package "Barcode::Da +taMatrix" (perhaps you forgot to load "Barcode::DataMatrix"?)
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.
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 ) );
Edit : For any future googlers : I went with Safe::Hole in the end and it seems to be doing what I need.

In reply to Safe.pm, Barcode::DataMatrix, Can't locate object method "new" via package "Barcode::DataMatrix" by Tharg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.