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

This likely has a simple answer, but it has me stumped. I'm trying to make an array reference, but when I use strict, I get the error:
Bareword "test" not allowed while "strict subs" in use
Here is my code:
use strict; my @test = (test,trial,good); my $var = "test"; push @{$var}, "Testing";
What am I doing wrong? Are there some other ways to do what I'm trying to do there?

Replies are listed 'Best First'.
Re: Problem with array references
by chromatic (Archbishop) on Sep 25, 2001 at 07:37 UTC
    First of all, everything you're assigning to @test needs to be quoted, whether explicitly or with a qw() construct.

    Second, your real problem is that you're using symbolic references. $var isn't an array reference. Dominus has written the quintessential explanation of what you're actually doing, and why it's a bad idea.

    The fix is to say:

    my @test = qw( test trial good ); my $var = \@test; push @{$var}, "Testing"; print join(' - ', @test), "\n";
    Relevant documentation is in perlref and perlreftut.
Re: Problem with array references
by vroom (His Eminence) on Sep 25, 2001 at 07:47 UTC
    Well your first problem is that you need to use quotes to wrap the items in your array.

    The second issue is that you are using symbolic references to get an array ref. That is to say you are looking up the reference points to the name of the array rather than a direct link to the array.

    Is there any reason you can't push onto a plain old array or a dereferenced hard reference?

    Here is an example with a hard reference;

    use strict; my @test=qw(test trial good); my $testref=\@test; push @$test, "Testing";


    vroom | Tim Vroom | vroom@blockstackers.com
      Yea, there are a few reasons. I posted a test case up there. Here is real code:
      push @$month, { DATES => $dates, CITY => $city, STATE => $state, TITLE => $title, LOCATION => $location, }
      The $month variable contains the name of the month. The data needs to be put in the appropriate array based on what the month variable contains. I could just make a giant if/then loop and do it, but I was looking for something shorter...