I’m writing a script in Perl that reads from a file and prints the contents.
It’s reading the file without errors, but it’s not printing the contents.
#!/usr/bin/perl
use warnings;
use strict;
use autodie;
my $dir = "/home/user/.fluxbox/.notify/notify";
if ( -e "$dir")
{
open(NOTE, "+>>", $dir) or die( "Error opening file! $!");
chomp(my @note = <NOTE>);
print "File Contents:\n";
print "@note\n";
close(NOTE) or die "Can't close $dir: $!";
}
Output:
Name "main::NOTE" used only once: possible typo at /path/to/script.pl line 13.
File Contents:
When you opening for appending that leaves the file offset at the end of the file, so reading will get you an immediate eof. You need to
seekbackwards (probably you want to the beginning of the file) before reading will get you anything. Since you’re not writing at all, you should probably just open the file for reading only (as others have said)