Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The <DATA> syntax in perl

Tags:

perl

Where can I find more about the following syntax in perl?

The connection between <DATA> and __DATA__ is unclear.

while (my $date_string = <DATA>) {   chomp($date_string);   next if not length $date_string;   print "$date_string ist Unixtime ",         $lang_date->str2time($date_string),         " und ",     $lang_date->time2str( '%d.%m.%Y %T (%Z)',$lang_date->str2time($date_string) ),         "\n"; }  __DATA__ 1.3.1999 1 Marz 1999 1. Marz 1999 1/3/1999 
like image 537
Skip Avatar asked Nov 19 '12 22:11

Skip


People also ask

What is the syntax of Perl?

A Perl variable name starts with either $, @ or % followed by zero or more letters, underscores, and digits (0 to 9). Perl does not allow punctuation characters such as @, $, and % within identifiers. Perl is a case sensitive programming language.

What is data type in Perl?

Perl has three data types: scalars, arrays of scalars, and associative arrays of scalars. Normal arrays are indexed by number, and associative arrays by string. The interpretation of operations and values in perl sometimes depends on the requirements of the context around the operation or value.

What are the data type that Perl support?

Perl has three basic data types: scalars, arrays of scalars, and hashes of scalars, also known as associative arrays. Here is a little detail about these data types.

What are the Perl variables write the syntax for each?

A scalar variable will precede by a dollar sign ($) and it can store either a number, a string, or a reference. An array variable will precede by sign @ and it will store ordered lists of scalars. Finaly, the Hash variable will precede by sign % and will be used to store sets of key/value pairs.


Video Answer


1 Answers

Quoting the doc:

The __DATA__ token tells the perl compiler that the perl code for compilation is finished.

Everything after the __DATA__ token is available for reading via the filehandle FOOBAR::DATA, where FOOBAR is the name of the current package when the __DATA__ token is reached.

This works just the same as __END__ does in package 'main', but for other modules data after __END__ is not automatically retrievable, whereas data after __DATA__ is.

Can add to this only that using __DATA__ section is quite handy to illustrate some file reading-related concepts in Perl. it's basically a file attached to a code, and contents of this file are easily accessible through <DATA>. That's why it's quite popular here on SO. )

like image 64
raina77ow Avatar answered Sep 22 '22 05:09

raina77ow