How to embed data in your Perl program

Here’s a sample Perl program that demonstrates how you can include (embed) data inside of your Perl program, right in there next to the source code.

This simple program takes the data after the special Perl __END__ tag, and makes it available to your Perl source code.

#!/usr/bin/perl

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

__END__
George Washington
Abraham Lincoln
John F. Kennedy

As you can see, you loop through the data with this line of code:

while (<main::DATA>)

Of course in a real program you’d probably do something a little harder inside of your data loop, so here’s something a little harder:

#!/usr/bin/perl

while (<main::DATA>)
{
  ($firstname, $lastname) = split;
  print "$lastname, $firstname\n";
}

__END__
George Washington
Abraham Lincoln
John Kennedy

Okay, admittedly that wasn’t great, but it’s all I have right now. :)