Suppose I have multiple input files, and would like to make their
contents part of the script, rather than having the script access them
as external files. If I have one input file, this is easy to do by
means of the __DATA__ token.
Can something similar be done with multiple input files?
A poster already pointed you to a relevant module. However, as usual
in Perl, TIMTOWTDI and since we don't know what are your real
requisites, we can't suggest which one is the best, provided such a
"best" exists. For example you could use HERE documents just as easily
as the DATA FH, but I really don't know...
In any case, since your files are small enough that you would find it
handy to put their contents into your code, it is reasonable to guess
it wouldn't be prohibitive to hold them in memory, would it?
Just see the following self-explanatory oversimplified example, maybe
you can adapt it to your needs...
#!/usr/bin/perl
use strict;
use warnings;
my @fh;
{
local $/=''; my $i;
open $fh[$i++], '<', \$_ for <DATA>;
}
print scalar <$_> for @fh;
__END__
aaa
bbb
ccc
un
due
tre
foo
bar
baz
OTOH, if you want to readline() from one of those FHs with the <>
operator, you can't do that directly, so you may want to play with the
symbol table:
#!/usr/bin/perl
use strict;
use warnings;
my @fh;
{
local $/=''; my $i;
open $fh[$i++], '<', \$_ for <DATA>;
no strict 'refs';
*{"DATA$_"}=$fh[$_] for 0..$#fh;
}
print scalar <$_> for @fh;
print "---\n", <DATA1>;
__END__
...
Be warned though that the usual cmts wrt the use of symrefs apply, so
in doubt, please ask here and most importantly, *please*, do *not*
abuse this technique...
HTH,
Michele