Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

So what exactly does <FILE> do?

Tags:

perl

So, I've used <FILE> a large number of times. A simple example would be:

open (FILE, '<', "someFile.txt");
while (my $line = <FILE>){
print $line;
} 

So, I had thought that using <FILE> would take a a part of a file at a time (a line specifically) and use it, and when it was called on again, it would go to the next line. And indeed, whenever I set <FILE> to a scalar, that's exactly what it would do. But, when I told the computer a line like this one:

print <FILE>;

it printed the entire file, newlines and all. So my question is, what does the computer think when it's passed <FILE>, exactly?

like image 907
Breaking Bioinformatics Avatar asked Jun 10 '14 13:06

Breaking Bioinformatics


People also ask

What does a file do?

A file is a container in a computer system for storing information. Files used in computers are similar in features to that of paper documents used in library and office files.

Why do we use a file?

A file may be designed to store an Image, a written message, a video, a computer program, or any wide variety of other kinds of data. Certain files can store multiple data types at once. By using computer programs, a person can open, read, change, save, and close a computer file.

What are the 3 types of files?

The types of files recognized by the system are either regular, directory, or special. However, the operating system uses many variations of these basic types. All file types recognized by the system fall into one of these categories. However, the operating system uses many variations of these basic types.

What happens when I open a file?

What happens when a file is opened? Each operating system has its technical process but generally, when you open a file, it follows the file extension and the directory. Each file contains an inode number and file name. An inode number is an identification number per file.


1 Answers

Diamond operator <> used to read from file is actually built-in readline function.

From perldoc -f readline

Reads from the filehandle whose typeglob is contained in EXPR (or from *ARGV if EXPR is not provided). In scalar context, each call reads and returns the next line until end-of-file is reached, whereupon the subsequent call returns undef. In list context, reads until end-of-file is reached and returns a list of lines.

If you would like to check particular context in perl,

sub context { return wantarray ? "LIST" : "SCALAR" }

print my $line  = context(), "\n";
print my @array = context(), "\n";
print context(), "\n";

output

SCALAR
LIST
LIST
like image 84
mpapec Avatar answered Sep 30 '22 22:09

mpapec