APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

2004/01/13 __DATA__

In a Perl script, this indicates the end of executable code. Whatever is beyond this is not interpreted or checked by "perl -c":


#!/usr/bin/perl
foreach (@ARGV) {
  print "$_\n";
  }
__DATA__

whatever you like {{{{
}
 

The stuff beyond __DATA__ can be accessed by your code:

#!/usr/bin/perl
foreach (@ARGV) {
  print "$_\n";
  }
while (<DATA>) {
  print;
}
__DATA__

whatever you like {{{{
}
 

That can be useful for small chunks of data you want to carry with a program, but it's also handy while writing code: stash samples from data files where you can easily review them, or read in code from elsewhere you want to cut and paste from.

Another use is in debugging: "perl -c" is pretty smart, and can often tell you the line number you screwed up on. However, if you are fumble-fingered like me, and type in a bunch of code at once without checking, you can get Perl so confused that it has no idea where you went wrong. I'm always doing something like this:

if ($something eq "red" } {
 ...

}
 

($something eq "red" should close with a ")", not "}").

Tuck a few of those in your code and "perl -c" won't help you much. So I isolate the problem by moving __DATA__ to just under the main code. If "perl -c" doesn't belch, the problem is in a subroutine somewhere. Move __DATA__ under the first subroutine and try again. Once it does complain, you know the problem is above - move __DATA__ up until you don't see it, and you've isolated the problem area. Sometimes I have to get into code sections, which may require adding a "}" on the line above, but I'll usually spot my error before it gets that bad.



Got something to add? Send me email.





Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





Technology is both a tool for helping humans and for destroying them. This is the paradox of our times which we're compelled to face (Frank Herbert).

Computers are useless. They can only give you answers. (Pablo Picasso)







This post tagged: