Using DATA with Multiple Input Files

M

Mark Shelor

Here's a problem I've encountered more than once, and didn't have a
clean solution:

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?

TIA, Mark
 
I

Iain Chalmers

Mark Shelor said:
Here's a problem I've encountered more than once, and didn't have a
clean solution:

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?

http://search.cpan.org/~dconway/Inline-Files-0.62/lib/Inline/Files.pm

Damian has solved your problem already :)

big
 
M

Mark Shelor

Iain said:


Thanks Iain. It looks like he has. I'm a bit concerned by the warnings
in the docs, however:

"WARNING

"It is possible that this module may overwrite the source code in files
that use it. To protect yourself against this possibility, you are
strongly advised to use the -backup option described in "Safety first".

"This module is still experimental. Regardless of whether you use
-backup or not, by using this module you agree that the authors will
b<under no circumstances> be responsible for any loss of data, code,
time, money, or limbs, or for any other disadvantage incurred as a
result of using Inline::Files."

Yikes!

Mark
 
A

Anno Siegel

Mark Shelor said:
[...]
Damian has solved your problem already :)
[...]

Thanks Iain. It looks like he has. I'm a bit concerned by the warnings
in the docs, however:

"WARNING

"It is possible that this module may overwrite the source code in files
that use it. To protect yourself against this possibility, you are
strongly advised to use the -backup option described in "Safety first".

"This module is still experimental. Regardless of whether you use
-backup or not, by using this module you agree that the authors will
b<under no circumstances> be responsible for any loss of data, code,
time, money, or limbs, or for any other disadvantage incurred as a
result of using Inline::Files."

Yikes!

Well, just from the module's function it must modify, i.e. overwrite, the
user's source. As a module author, you *must* cover your ass if you do
that. If some clown blames anything that happened to their source to the
module, you'd have a hard time proving you're innocent.

Damian is a more-than-competent programmer, the module won't mess up
anything. Just don't assign your backup to /dev/null :)

Anno
 
M

Michele Dondi

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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top