in reply to Prevent import of subroutines from eval

I have a program that is eval-ing the content of a lot strings

(Emphasis added.) Defining a new package name for each one is going to just keep adding memory consumption.

You might want to fork() and have the child evaluate the code. Or you might want to, every so often, re-exec the script to reset the memory consumption. Which is easier depends on which is more complicated: the data you get out of running the eval'd code or the program's "state" so that the new instance can properly continue where the prior instance left off.

use strict; my $pid = open my $pipe, "-|"; die "Can't fork: $!\n" if ! defined $pid; if( ! $pid ) { my $output = eval ...; print $output; exit; } local $/; my $output = <$pipe>; close $pipe;

Or

if( been_a_while() ) { system( $^X, $0, "--restart=$context", @ARGV ); }

- tye        

Replies are listed 'Best First'.
Re^2: Prevent import of subroutines from eval (delete namespace)
by LanX (Saint) on Oct 11, 2013 at 14:24 UTC
    > Defining a new package name for each one is going to just keep adding memory consumption.

    so what about just deleting the namespace?

    DB<123> ; {package TMP; eval 'sub bla {"BLA"}'} DB<124> TMP::bla() => "BLA" DB<125> delete ${'main::'}{'TMP::'} => do { my $a = *main::TMP::; $a = { bla => *TMP::bla }; $a; } DB<126> TMP::bla() Undefined subroutine &TMP::bla called at (eval 72)[multi_perl5db.pl:64 +4] line 2.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re^2: Prevent import of subroutines from eval (fork or exec)
by Swandog (Initiate) on Oct 11, 2013 at 16:44 UTC

    Well, fortunately, the program is not particularly long running (< 3 minutes), so memory consumption is not a huge concern to me.

    I might have overreached with my use of the phrase "a lot". I'm talking about 30 or so.