I don't see anything wrong with your code although I have heard that at some point bare word file handles are going to be deprecated.
I don't think "resetting" the __DATA__ input is what you want for multiple tests although that is possible.

The DATA file handle is a pre-opened file handle to your Perl script that is pre-seeked to first byte of the line right after __DATA__. To cause a Perl program to read itself, you seek the DATA handle back to be beginning (byte 0) and then print all lines. If you want to just re-read the __DATA__ segment, you can use "tell" to find out the byte position where the DATA segment starts, save that number and then seek to that byte number instead of to byte #0 for the re-read operation.

Another option is to use variables for the I/O. Note that you can open a scalar for "write" - I would NOT advise doing that with a DATA segment as you are liable to wind up scribbling over your Perl program!
Consider the following code:

use strict; use warnings; my $data_set_name; my $another_data_set; open my $data2, '<', \$data_set_name or die "some message $!"; print $_ while (<$data2>); print "\n"; open my $data3, '<', \$another_data_set or die "some message $!"; print $_ while (<$data3>); print "\n"; print "NOW READING MYSELF...\n"; seek(DATA,0,0); print $_ while (<DATA>); # Using BEGIN blocks allows potentially lengthy data # to appear at the end of the program file BEGIN{ $data_set_name = <<END; asdf qerg 5666 END } BEGIN{ $another_data_set = <<EOF 46464 9187 jjh EOF } __DATA__
Another option is to use Inline::Files. I have used that module before and there can be unexplained weirdness with it! For example, it won't co-exist with the above code. I think because Inline::Files plays some fancy games with BEGIN.

Added: You can write your code using a lexical file handle, my $input_fh = \*STDIN and then of course set $input_fh = $data2;, etc..


In reply to Re: STDIN typeglob by Marshall
in thread STDIN typeglob by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.