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?
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With