I want the user to input a file name and have the program output the contents of the file.
I get 2x “Unquoted string may clash with future reserved word” and “Global symbol $filename requires explicit package name.
use strict;
use warnings;
print 'Enter file name: ';
my $fileName = <STDIN>;
chomp($fileName);
open(fh, $filename or die $!);
Also, why is file handle not a variable (actually depending on the example I’ve seen it a variable or not, like what I have here)?
All your problems are right here (and
$filenamevs.$fileName).You want something more like
In the first case, the
diecondition is on$filename; it’s equivalent to:But the second one is more like:
Although doing it this way (with
if (!open)) is pointless unless you declare$fhwith a more useful scope — right now it’s only visible inside theifblock.The second case implicitly covers the first case, since if
$filenameis undefinedopen()will fail withUse of uninitialized value $filename in open.