Put Perl test data in the same file as your source code

Perl test data FAQ: How can I store some sample/test data with my source code in Perl?

Answer: With Perl it's very easy to store some sample data in the same file as your Perl source code. Assuming you're creating a "main" Perl program (not a Perl module) you can use the special __END__ operator, and then include your data after that operator, as shown in my sample code below. You can then access that data using the main::DATA operator inside of a while loop.

Here's a quick sample program:

#!/usr/bin/perl

while (<main::DATA>)
{
  print $_;
}

__END__
Four score and seven years ago
our fathers
did x, y, and z

Using __DATA__ or __END__

I've read that you're supposed to use the string __DATA__ when enclosing data at the end of a Perl module and __END__ when you want to include data in a separate, standalone Perl program, but I just noticed that you can also use the string __DATA__ instead of __END__ in a main program. Typically I just use this approach to test my program, or use it as a convenient way to share my code and some sample data with someone else, so I haven't ever tried it in a reusable module.