I found this old Perl code in an old unix book and was surprised it works. What is the relationship between $FILE
and FILE
in the code below?
#! /usr/bin/env perl
print 'Enter filename: ';
chomp($FILE = <STDIN>);
open(FILE); # what is going on here?
while (<FILE>) {
print;
}
close(FILE);
exit(0);
open
has several different tricks depending on the number of arguments you give it. In the one-argument case such as open(FILE)
, Perl will take the filename from the scalar package variable with the same name as the bareword filehandle. In this case, using only FILE
causes Perl to look in $FILE
. See Calling open with one argument via global variables.
This fell out of favor for several reasons, including: