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

If I have a line as shown below, is there any way that I can get the script to execute the contents of the variable?

my $var = "print \"hello\\n\"\;";
  • Comment on Running a Perl script stored in a variable

Replies are listed 'Best First'.
Re: Running a Perl script stored in a variable
by FunkyMonk (Bishop) on Mar 28, 2008 at 17:24 UTC
    Danger, Will Robinson!

    eval will do this, but be very sure you know the contents of your string are safe.

      Oh, thanks a bunch.

      I understand the risk of the string contents being incorrect/unsafe. However, the script will be used in a controlled test environment where the string contents would be guaranteed to be correct.

      Cheers,
      Deiva
        Now I'm curious: why would you do this rather than simply running the script?
Re: Running a Perl script stored in a variable
by jfraire (Beadle) on Mar 28, 2008 at 18:26 UTC

    Try using quote operators such as here-docs or q{} in order to avoid some debugging time when one of your strings fail... See perlop.

    Regards,

    Julio