hello

I've got a directory full of perl scripts that declare functions. These files are loaded into a Perl app-server when it starts up, but if any of the files either have compile errors, or don't end in a true value, the app server will stop but not start => stressed developers, grumpy editors.

To avoid this unhappy scenario, I wrote a simple test script that slurps each file in turn and uses a Safe to eval the code and catch errors before someone presses the big red restart button. I used Safe rather than eval because I wanted to prepare and manage the namespace in which the files are run, and protect the test's own namespace. I trust the code, um, morally, but I'm pretty suspicious of a lot of the older code's habit of stomping around in main::.

However, I'm finding what works in Safe on my own machine (Perl 5.8.0) doesn't work on the production box (5.6.0). I'm doing something like:

#!/usr/bin/perl use Safe; use Opcode; my $code; { local $/; $code = <DATA> } my $box = Safe->new(); $box->permit(Opcode::full_opset); my $return_code = $box->reval($code); __DATA__ use strict; use vars qw($foo @bar); $foo = "Friday 13th: unlucky for some";

Though this works fine in 5.8, when I run this is 5.6, it complains that '@bar requires explicit package name'. It seems that although Safe is allowing the 'use strict' declaration, it's blocking 'use vars qw(....)'.

My petition is: how can I get round this? I don't properly understand opcodes, have I done something wrong here? Or can I run this test in a different way? I'm not averse to using eval, so long as the test can control the namespace of the tested eval, and, crucially, that I don't introduce any ordering dependency into running the tests.

With thanks
ViceRaid


In reply to Strict, safely by ViceRaid

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.