Gathering Source files

R

Ray Muforosky

Help.

If I have file1 that loads (require file2 and files), is it possible
save the content of file file after the load.

example:
file1
#!perl
$a =12;
require file2;
$b = $z;
require file3;
$c = $y;
print $a;


file2
$z = 200;
1;

file3
$y = "foo";
1;

So, is it possible to save file1 after loading file2 and file3
file1 becomes:
#!perl
$a =12;
$z = 200;
$b = $z;
$y = "foo";
$c = $y;
print $a;


Thanks
Ray
 
X

xhoster

Ray Muforosky said:
Help.

If I have file1 that loads (require file2 and files), is it possible
save the content of file file after the load.

example:
file1
#!perl
$a =12;
require file2;
$b = $z;
require file3;
$c = $y;
print $a;

file2
$z = 200;
1;

file3
$y = "foo";
1;

So, is it possible to save file1 after loading file2 and file3
file1 becomes:
#!perl
$a =12;
$z = 200;
$b = $z;
$y = "foo";
$c = $y;
print $a;

Those are not (in general) the same thing, because of the file scoping
of lexical variables and such. I know that in the example you give there
are no lexical variables, but then again in the example you give it is
trivially easy to do what you want by hand. So I have to assume that the
real world extends beyond your example.

A not very generic (or good) solution would be something like this:

warn "Untested";
$program1 =~ s/\brequire ([^;]+);/"\n".get_file($1)."\n"/eg;

sub get_file {
## see perldoc -f require for how to find file when not in cwd.
open my $fh, "<", "$_[0].pm" or die "$_[0]: $!";
local $/;
return <$fh>;
};

Among other things that would break this would be quoted things that have
the word "require" within them, so look at Filter::Simple and such modules
to fix that.

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

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top