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

I'm trying to create a sub within a package and here's my code so far
my ($self) = @_; my $templateName = (defined $self->{FormConfig}{template} ? $self +->{FormConfig}{template} : ''); if (-e $templateName) { open (FILE, $templateName); $self->@File = <FILE>; close (FILE);
and here's the error I'm getting
Array found where operator expected at (eval 5) line 1150, at end of l +ine (Missing operator before ?) syntax error at (eval 5) line 1150, near "->@File " Global symbol "@File" requires explicit package name at (eval 5) line +1150.

Replies are listed 'Best First'.
Re: declaring an array within a package
by sauoq (Abbot) on Dec 19, 2003 at 00:16 UTC

    The error you got was very explanatory. It told you exactly where the problem is...

    $self->@File = <FILE>;

    What made you think that would work? Did you just make that syntax up and wish yourself good-luck?

    I think you probably want something like this

    @{$self->{File}} = <FILE>;
    instead.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: declaring an array within a package
by Joost (Canon) on Dec 19, 2003 at 00:19 UTC
Re: declaring an array within a package
by Zaxo (Archbishop) on Dec 19, 2003 at 00:17 UTC

    The scalar $self is a reference to something if you mean to bless it into a class defined in your package. The statement $self = @_; sets it equal to the number of arguments. That is not the cause of the message, but it indicates you need to study perl references.

    The statement $self->@File = <FILE>; is semantically incorrect. You may mean something like         $self->{'File'} = [<FILE>]; which puts a reference to an array of lines from the file into $self's 'File' data element.

    Update: Oops, didn't check my memory against the question. sauoq++ is correct, of course. Doesn't change the argument, just the value $self gets.

    After Compline,
    Zaxo

      The statement $self = @_; sets it equal to the number of arguments.

      Well, that statement would, but that statement didn't appear in the OP's post. He had my ($self) = @_; which is list context and assigns the first element of @_ to $self.

      -sauoq
      "My two cents aren't worth a dime.";