Using DATA with Multiple Input Files

Discussion in 'Perl Misc' started by Mark Shelor, Jan 28, 2004.

  1. Mark Shelor

    Mark Shelor Guest

    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
    Mark Shelor, Jan 28, 2004
    #1
    1. Advertising

  2. In article <>,
    Mark Shelor <> wrote:

    > 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

    --
    'When I first met Katho, she had a meat cleaver in one hand and
    half a sheep in the other. "Come in", she says, "Hammo's not here.
    I hope you like meat.' Sharkey in aus.moto
    Iain Chalmers, Jan 28, 2004
    #2
    1. Advertising

  3. Mark Shelor

    Mark Shelor Guest

    Iain Chalmers wrote:
    > In article <>,
    > Mark Shelor <> wrote:
    >>
    >>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 :)



    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
    Mark Shelor, Jan 28, 2004
    #3
  4. Mark Shelor

    Anno Siegel Guest

    Mark Shelor <> wrote in comp.lang.perl.misc:
    > Iain Chalmers wrote:
    > > In article <>,
    > > Mark Shelor <> wrote:
    > >>
    > >>Suppose I have multiple input files, and would like to make their
    > >>contents part of the script, rather than having the script access them


    [...]

    > > 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
    Anno Siegel, Jan 28, 2004
    #4
  5. On Tue, 27 Jan 2004 18:43:25 -0700, Mark Shelor
    <> wrote:

    >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
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
    Michele Dondi, Jan 29, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Brennan
    Replies:
    27
    Views:
    8,144
    Ron Brennan
    May 12, 2004
  2. Replies:
    4
    Views:
    936
    M.E.Farmer
    Feb 13, 2005
  3. Moskie
    Replies:
    0
    Views:
    383
    Moskie
    Aug 4, 2006
  4. Walter
    Replies:
    6
    Views:
    228
    Bob Barrows [MVP]
    Sep 17, 2004
  5. Ron Brennan
    Replies:
    5
    Views:
    305
    Dr John Stockton
    May 14, 2004
Loading...

Share This Page