> From: activeperl-boun...@listserv.activestate.com [mailto:activeperl-
> boun...@listserv.activestate.com] On Behalf Of Phillip Richcreek
> Sent: 02 May 2012 14:56
> To: activeperl@listserv.activestate.com
> Subject: Inline test data
>
> A recent post used a nice feature for providing test data to a script.
> (See __DATA__ below) I've seen it before but never understood it. How
> does it work? What signals the end of the data to the  while (my $line
> = <DATA>) { ? Is this a type of here document? Can someone point me
> where it's descibed in the perl doc?

That's a good question. It took me a while to find it, but see the Special 
Literals section in 'perldoc perldata'.

The __DATA__ is just a marker to tell the perl compiler that there is no more 
code after this point. The DATA file handle is the same one used by the 
compiler when it stopped parsing (or a duplicate), so the next read will be the 
first line after __DATA__. You can read all the way to the end of the source 
file. In fact you can rewind (perldoc -f seek) the DATA file handle and read 
the script from the beginning, if you want. For (a trivial) example...

use strict;
use warnings;

seek DATA, 0, 0;
while (<DATA>) {
    print;
}
__DATA__

HTH

--
Brian Raven

Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are 
not the intended recipient or have received this e-mail in error, please advise 
the sender immediately by reply e-mail and delete this message and any 
attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to