in reply to eval a perl script

This sort of solution has been discussed before - the biggest problem is that BEGIN blocks don't quite get handled properly (as I understand it) - but this shouldn't be a problem for you, since your not writing a Perl application dependent on another Perl application. You're just writing a wrapper.

However, I wonder at the situation that prevents you from using Perl - perl2exe simply wraps the Perl program in the perl environment - effectively your carrying Perl around, so why not just get the real thing? Also, if you KNOW that you are only running ONE Perl program, why not perl2exe it instead of a writing a wrapper?

Assuming that you know the answers to the preceeding questions, then, yes this is what you are looking for. Here is a slightly more efficient variant.

package Wrapper; #To prevent interfering with the eval'ed Program our ($code); my $prog = shift; open PROGRAM, $prog or die "Can't open $prog: $!"; { local $/; undef $/; $code = <PROGRAM> } close PROGRAM; eval $code or die "$@";
Cheers,
Erik

Update: Pay no attention to this crappy code, look at Juerd's cleaned up version. These aren't the droids you are looking for. Move along.

Replies are listed 'Best First'.
Re: Re: eval a perl script
by Juerd (Abbot) on Apr 14, 2002 at 22:17 UTC

    open $prog, PROGRAM

    I think you meant open PROGRAM, $prog.

    package Wrapper; #To prevent interfering with the eval'ed Program

    There's not much point in doing so if you don't switch back the package to main in the evaled code. Having $code as a global is good, but the lexical $prog will also be visible in evaluated code.

    eval $code or die "$@";

    You expect the code to return a true value (eval returns the last evaluated expression, just like a do-block), but that's not very common for a non-module. It's better to check $@ itself.

    package Wrapper; { local $/ = undef; open PROGRAM, shift or die "Can't open $prog: $!"; $Wrapper::code = <PROGRAM>; close PROGRAM; } eval 'package main; ' . $Wrapper::code; die $@ if $@;

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.