I need to add unit testing to some old scripts, the scripts are all basically in the following form:
#!/usr/bin/perl # Main code foo(); bar(); # subs sub foo {
Assuming you have no security concerns, wrap it in a sub { ... } and eval it:
use File::Slurp "read_file"; eval "package Script; sub {" . read_file("script") . "}"; is(Script::foo(), "foo");
(taking care that the eval isn't in scope of any lexicals that would be closed over by the script).