Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a content of a file to the screen in perl

Tags:

file

perl

I Have a perl script which write a few lines into file. (I checked and see that the file is written correctly) right after that I want to print the content to the screen, the way I'm trying to do it- is to read the file and print it

open (FILE, '>', "tmpLogFile.txt") or die "could not open the log file\n";
$aaa = <FILE>; 
close (FILE);  
print $aaa;

but I get nothing on the screen, what do I do wrong?

like image 863
user4409082 Avatar asked Dec 31 '14 19:12

user4409082


People also ask

How do I print the contents of a file in Perl?

print() function is used to write content to a file. Here, filehandle is associated to the file at the time of opening the file and string holds the content to be written to the file.

How do I open a file for reading in Perl script?

Perl open file function You use open() function to open files. The open() function has three arguments: Filehandle that associates with the file. Mode : you can open a file for reading, writing or appending.


1 Answers

To read you need to specify the open mode as <. Also, $aaa = <FILE> has scalar context, and only reads a line. Using print <FILE> you can have list context and read all lines:

open (FILE, '<', "tmpLogFile.txt") or die "could not open the log file\n";
print <FILE>;
close (FILE);
like image 155
Jens Avatar answered Oct 02 '22 20:10

Jens