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

I get this error, anyone know why?
Can't use string (">gamelog.txt") as a symbol ref while "strict refs" +in use at shannon.pl line 31, <STDIN> line 1.
my $gamesave = "gamesave.txt"; open(LOG, ">$gamesave") or die "Error: $!"; print LOG join(" ", @secrettext_new); close(LOG) or die "Error: $!"

Replies are listed 'Best First'.
Re: Opening a file for writing
by shmem (Chancellor) on Jul 13, 2007 at 13:01 UTC
    Which of your code lines is line 31 of shannon.pl? Not the ones you posted, I deem, since you clearly stick gamesave.txt into your variable $gamesave, but perl croaks about gamelog.txt.

    That error is thrown if you try to do

    use strict; my $file = ">gamelog.txt"; open $file, $file or die;

    which you appear to be doing elsewhere in your code.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Opening a file for writing
by rpanman (Scribe) on Jul 13, 2007 at 12:36 UTC
    Works fine for me as this code:
    use strict; use warnings; my $gamesave = "gamesave.txt"; my @secrettext_new; open(LOG, ">$gamesave") or die "Error: $!"; print LOG join(" ", @secrettext_new); close(LOG) or die "Error: $!"
    ... the only change I made was to define @secrettext_new to avoid warnings.

    Maybe it is something in the preceding lines that is the problem.

    My perl version is:
    This is perl, v5.8.8 built for sun4-solaris-thread-multi
    Update: Added perl version