Novice Q: Can't use string ("X") as a symbol ref

Discussion in 'Perl Misc' started by Yevgeniy K., Oct 14, 2009.

  1. Yevgeniy K.

    Yevgeniy K. Guest

    I have the following code to open a file:

    use strict;

    sub open_file {
    my ($file, $FILE) = @_;
    unless (open ($FILE, ">>$file")) { die "error msg\n" }
    }

    And at runtime I get: "Can't use string ("LIST") as a symbol ref while
    "strict refs" in use"

    Maybe my understanding of references is incorrect, but I fail to grasp
    why this is a reference. I assign the value of local scalar variable
    $FILE to the second argument passed to the subroutine, which in this
    case happens to be "LIST"; then use that scalar var instead of a
    literal to set the filehandle--what is the problem? Please advise!
    Thanks in advance!
    Yevgeniy K., Oct 14, 2009
    #1
    1. Advertising

  2. Yevgeniy K.

    Guest

    On Wed, 14 Oct 2009 15:36:56 -0700 (PDT), "Yevgeniy K." <> wrote:

    >I have the following code to open a file:
    >
    >use strict;
    >
    >sub open_file {
    > my ($file, $FILE) = @_;
    > unless (open ($FILE, ">>$file")) { die "error msg\n" }
    >}
    >
    >And at runtime I get: "Can't use string ("LIST") as a symbol ref while
    >"strict refs" in use"
    >
    >Maybe my understanding of references is incorrect, but I fail to grasp
    >why this is a reference. I assign the value of local scalar variable
    >$FILE to the second argument passed to the subroutine, which in this
    >case happens to be "LIST"; then use that scalar var instead of a
    >literal to set the filehandle--what is the problem? Please advise!
    >Thanks in advance!


    Its all mucky stuff, barewords, symbols, filehandles.
    Any scalar can be used in an open, it depends upon whats in the variable
    and if "strict refs" are in effect.

    These are some options. What's in the textfile.txt after you do the below?

    my $FILE = "LIST";
    open ($FILE, '>>', "textfile.txt") or die $!;
    print $FILE "\"LIST\"\n";
    close $FILE;

    use strict;
    use warnings;

    $FILE = *LIST;
    open ($FILE, '>>', "textfile.txt") or die $!;
    print $FILE "*LIST\n";
    close $FILE;

    $FILE = \*LIST;
    open ($FILE, '>>', "textfile.txt") or die $!;
    print $FILE "\\*LIST\n";
    close $FILE;

    $FILE = undef;
    open ($FILE, '>>', "textfile.txt") or die $!;
    print $FILE "undef\n";
    close $FILE;

    my $FOO = $FILE;
    open ($FOO, '>>', "textfile.txt") or die $!;
    print $FOO "FOO = FILE\n";
    close $FOO;

    $FILE = "LIST";
    open ($FILE, '>>', "textfile.txt") or die $!;
    print $FILE "\"LIST\"\n";
    close $FILE;

    -sln
    , Oct 15, 2009
    #2
    1. Advertising

  3. Yevgeniy K.

    Yevgeniy K. Guest

    Thank you for the responses!!
    Yevgeniy K., Oct 15, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    335
  2. Replies:
    22
    Views:
    751
    peter koch
    Apr 30, 2008
  3. Replies:
    6
    Views:
    344
    James Kanze
    Apr 29, 2008
  4. Navindra Umanee

    strong ref from weak ref?

    Navindra Umanee, Feb 12, 2005, in forum: Ruby
    Replies:
    2
    Views:
    143
    Navindra Umanee
    Feb 12, 2005
  5. Juha Nieminen
    Replies:
    13
    Views:
    608
    Edek Pienkowski
    Aug 29, 2012
Loading...

Share This Page