From
perldata,
...the tokens __END__ and __DATA__ may be used to indicate the logical end of the script before the actual end of file. Any following text is ignored...
Text after __DATA__ but may be read via the filehandle PACKNAME::DATA
So essentially sticking
__END__ at line 5 in your script tells the perl interpreter that is the end of your script. Or to put it another way, every line after line 5 is now "commented out".
The
__DATA__ token isn't usually used much in real-world tasks, but I think is very useful for creating a self-contained portable example of an issue you might be having, which you can then include with your question on PerlMonks.
#! usr/bin/perl
use strict;
use warnings;
my $search_term = 'Smith';
#no need to open the DATA handle, you can simply start reading it...
for my $line (<DATA>){
print $line if $line =~ m{$search_term} ;
}
__DATA__
Fred Smith
Jane smith
Bob Smitters
Joe Smith
__END__
This is the output I'm getting:
C:\Temp>perl test.pl
Fred Smith
Joe Smith
But I was expecting:
Fred Smith
Jane smith
Joe Smith
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.