link867 has asked for the wisdom of the Perl Monks concerning the following question:

I have been having the hardest time with the open function on the server I am coding. I had no problem with it on my VM but because of restrictions of the proxy server I have to use a server to script. This is the output of perl -v: This is perl, v5.8.0 built for i586-pc-sco3.2v5.0 and unname -a: SCO_SV 3.2 5.0.7 i386 I keep getting Can't use an undefined value as a symbol reference at ./templateChecker.pl line 97 Here is the problem sub:

sub getComment { my $switchIp = $_[0]; my $INFILE; open ($INFILE, 'templateHosts' ) or die "can't open file $!"; my @hostsText = <$INFILE>; my $hostsLine = "";
I have also tried different ways :
open ($INFILE, '<', 'templateHosts') open ($INFILE, '< templateHosts') open $INFILE, '<templateHosts')
Thanks for any help!

Replies are listed 'Best First'.
Re: Undefined value as a symbol reference
by kennethk (Abbot) on Jul 21, 2014 at 20:20 UTC
    Have you tried a three-argument open?
    open( my $INFILE, '<', 'templateHosts') or die "can't open file $!";
    Also, are you certain that you've replicated the code exactly? From open:
    If FILEHANDLE is an undefined scalar variable (or array or hash element), a new filehandle is autovivified, meaning that the variable is assigned a reference to a newly allocated anonymous filehandle. Otherwise if FILEHANDLE is an expression, its value is the real filehandle. (This is considered a symbolic reference, so use strict "refs" should not be in effect.)
    It looks like the interpreter is regressing into this symbolic reference behavior, though this would presuppose your value is not null.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Yes, I copied the syntax directly from the perldocs man page to get the two argument. When I try the three argument I get: Too many arguments for open at ./templateChecker.pl line 97, near "'templateHosts'
        It looks like this is a version issue. I tracked down the 5.8.0 documentation, and according to that version of perlopentut, it looks like three-argument open and indirect file handles were not yet implemented. You could try to use IO::Handle, or stick with bare word file handles, e.g.
        open (INFILE, 'templateHosts' ) or die "can't open file $!";
        or explicitly define your glob with Symbol:
        use Symbol; my $INFILE = gensym; open ($INFILE, 'templateHosts' ) or die "can't open file $!";
        You'll probably have to explicitly close your filehandle assuming you want that done before end of execution. I'd probably go for the last option, as it's the closest to modern syntax. Actually, I'd try and raise enough fuss to upgrade to a modern version of Perl, but I assume that's not an available option....

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Undefined value as a symbol reference
by link867 (Initiate) on Jul 21, 2014 at 20:39 UTC

    Here is the code with number lines:

    93 sub getComment 94 { 95 my $switchIp = $_[0]; 96 my $INFILE; 97 open ($INFILE, '<', 'templateHosts' ) or die "can't open f +ile $!"; 98 my @hostsText = <$INFILE>;