in reply to STDIN typeglob
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:
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.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__
Added: You can write your code using a lexical file handle, my $input_fh = \*STDIN and then of course set $input_fh = $data2;, etc..
|
---|