# untested my $code = 'while (<>) { print; }'; # would typically be more interesting my $output = ''; # storage for output my $input = "more and more\nstuff here"; # would typically be more interesting { tie *STDIN, 'IO::Scalar', \$input; tie *STDOUT, 'IO::Scalar', \$output; eval "$code"; } # interesting stuff should be in output here