exec dynamic script, capture output, but see vars of caller?

S

Stan R.

Hello, I've been tasked with writing a Perl program that generates
another Perl script on the fly (by parsing a file, whose name is passed
to the base script, that is basically a Perl program interweaved in a
document, where the Perl code it inside special tokens (sorta kinda like
how php looks like sitting among html markup.))

I can very easily parse the page using a couple of s/// statements and
such (god I love Perl's native RegEx :) ) as needed and either execute
with an eval() or dump the code into a file and execute it that.

Now an additional requirement has been tasked on me. The parsed code
needs to be able to 'see' global variables defined in the base script
(most of which comes from a use'ed package lib.)

I thought maybe I have solved this by simply dumping the parsed code
into a file and using it with require, which /does/ work until there's
an error in the code.

I can check the syntax with `perl -c generatedscript.pl` but that's on
compile time. I need to be able to catch runtime errors and warning as
well, in addition to catching it's output, and if there are and errors
or warning, they are to be output in sort of a notice instead of the
output, alerting the end user to a problem in code in the processed
file.

So, I pretty much have all the internals worked out, just this
requirement kinda changes things one me and I've been searching for ours
for a viable (and portable) solution.

Thanks for any advise on this.
 
X

xhoster

Stan R. said:
Hello, I've been tasked with writing a Perl program that generates
another Perl script on the fly (by parsing a file, whose name is passed
to the base script, that is basically a Perl program interweaved in a
document, where the Perl code it inside special tokens (sorta kinda like
how php looks like sitting among html markup.))

I can very easily parse the page using a couple of s/// statements and
such (god I love Perl's native RegEx :) ) as needed and either execute
with an eval() or dump the code into a file and execute it that.

Now an additional requirement has been tasked on me. The parsed code
needs to be able to 'see' global variables defined in the base script
(most of which comes from a use'ed package lib.)

I thought maybe I have solved this by simply dumping the parsed code
into a file and using it with require, which /does/ work until there's
an error in the code.

That sounds like a good reason not to use require. But why not use eval
or do?
I can check the syntax with `perl -c generatedscript.pl` but that's on
compile time. I need to be able to catch runtime errors and warning as
well, in addition to catching it's output, and if there are and errors
or warning, they are to be output in sort of a notice instead of the
output, alerting the end user to a problem in code in the processed
file.

Did you have the warnings part worked before you discovered the require
didn't do what you want? If so, why wouldn't that work with eval or do?

One way would be to do add something like this into the code you are
making:

local *STDERR;
open STDERR, ">wherever.err" or die $!;
local *STDOUT;
open STDOUT, ">whereever.out" or die $!;

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top