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

Hi,

On one machine, I want to run a single perl script...but I can't install perl.

I plan on using perl2exe (or perlapp) and then read the file and eval() it.

That way, the machine in question, will only have two files. The interp.exe program and the script.pl file...

This seems to work, but I am sure I am missing something...
use AnyModulesHere.pm; # Shift off first arg, rest will be used by # the "real" script my $script = shift @ARGV or die "Error: must pass filename\n"; my $code; open (FILE, "< $script") or die "Error: $!\n"; $code .= $_ while (<FILE>); close (FILE); eval $code; print $@ if $@;

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

    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.

      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.
      

Re: eval a perl script
by Juerd (Abbot) on Apr 14, 2002 at 21:45 UTC

    The interp.exe program and the script.pl file...

    Why not have "the perl.exe program" and the script.pl? :)

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