ViceRaid has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Strict, safely
by broquaint (Abbot) on Jun 13, 2003 at 09:53 UTC
    That works fine for me here (v5.6.1 built for i386-linux). Which version of Safe are you using and are you using 5.6.0 or 5.6.1 (the output of perl -v would be helpful)? This doesn't appear to be an opcode issue as Safe usually issues a ... trapped by operation mask if there's a problem with the opcodes.
    HTH

    _________
    broquaint

      No problems on:

      [alexf@localhost ~]$ perl -v This is perl, v5.8.0 built for i386-linux-thread-multi [alexf@localhost ~]$ perl -MSafe -e 'print $Safe::VERSION' 2.07

      but not happy on:

      [alexf@otherhost ~]$ perl -v This is perl, v5.6.1 built for sun4-solaris [alexf@otherhost ~]$ perl -MSafe -e 'print $Safe::VERSION' 2.06

      Thanks
      ViceRaid

Re: Strict, safely
by ViceRaid (Chaplain) on Jun 13, 2003 at 15:05 UTC

    With thanks to broquaint, it seems like it might be down to differences in the versions of Safe installed. Unfortunately, I can't seem to find the CHANGES file for Safe on CPAN to verify whether or not this is the case.

      Is this what you are looking for? Looks like the 2.09 version included a change which Made it work on 5.6 atleast, maybe even on 5.005?, so that may be what was causing your problems.

        Thanks, but not quite. I was looking for the changes between 2.06 and 2.07, as these are the versions which aren't and are working, respectively. I tried moving the newer version (2.07) onto the older, but the newer version doesn't like to play with the older version of Opcode. Thanks again for the link.

        ViceRaid